Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Help creating new block for custom module
 
SwiftCol
Member
 
Total Posts:  75
Joined:  2008-05-22
 

Hello all,
I recently used the module creator in this wiki to create a new module. The script ran great, but now i’m ready to move onto the next step.

What i want to do:
If you look at the Customer module, it has a few different sections, Customer Accounts, Customer Groups, and Online Customers. As far as I can tell, these sections are blocks. Am i correct in calling these blocks? I want to create something similar.

Some problems i’m having:
One problem that i’m having in trying to understand custom modules vs built-in modules, is that the URI and naming scheme are different. Where normal Magento modules are index.php/admin/<module>, custom modules are index.php/<module>/adminhtml_<module>. One thing I can’t figure out is: Is the second instance of the module name in the URL actually the block name? (index.php/<module>/adminhtml_<block>)

Summed up, my question is What parts of the custom module files determine the block, and which do not.

Thank you for any help you can provide.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

The URI and naming scheme is actually not really that different, the only difference is on the backend usually.  Let’s say that you create a namespace and module named LC_News.  The url to access on the frontend would be:

Frontend:

www.example.com/news/

Backend:

www.example.com/index.php/news/adminhtml_news/

The reason being for the backend is the necessity to tell Magento to look to the sub-directory in controllers and Block in the adminhtml directory.  We put the files there so that we can have all of the files in one place (all in the custom module) but in doing so it means we have to call it correctly in the backend, so it doesn’t quite follow the exact Magento naming convention.  Now their may be a way to rewrite it using the Routers (look up Zend Framework Router’s) which is a PHP interface to mod_rewrite in a sense.  Also note that you see index.php in the backend and not the frontend after calling it, that is because on the frontend we are using a .htaccess rule to remove the index.php.  Due to generally accepted programming practices using index.php as the entry way is a good way to help secure the overall platform.  You can write a similar .htaccess rule for the backend.

A lesson I learned in trying to access anything other than the default indexAction is where to go.  Let’s say you have these functions defined in your Adminhtml controller (IndexController.php):

public function indexAction()
{
...
}

public function viewAction()
{
...
}

One might logically think that you would go to:

www.example.com/index.php/news/adminhtml_news/view/

In reality you want to go to:

www.example.com/index.php/news/adminhtml_news/index/view/

The different sections of the Customer module are different blocks controlled by different controllers.  You can clearly see the structure by looking in Adminhtml at the Customer sections of Block and controllers.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
SwiftCol
Member
 
Total Posts:  75
Joined:  2008-05-22
 

Thank you Adam, while that does clear up a lot of things, (your ‘view’ example will be very helpful), I am still left unable to create a new block. In my case, I may be able to use your ‘view’ example instead of creating a new block for each page in my custom CMS, but I would still like to know how to create a new block.

I have tried copying much of the same folders and scripts that my module’s primary block use, but since the primary block and the module it’s self share the same name, I am having a very hard time getting it to work.

I will continue with the idea of making multiple pages using your ‘view’ example until I figure blocks out, but I am confused on the difference between the IndexController and the <Module>Controller files. I know that the <Module>Controller file controlls what I see in the backend, but what does the IndexController do?

Thank you again for all of your help. You’re saving me a lot of headache.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Oops, sorry I meant that the <Module>Controller is frontend and the IndexController is backend.  I actually just defaultly named the controller that based on previous stuff I cobbled together but I don’t know what that would do for access, I will experiment a bit and see what I can work with.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
SwiftCol
Member
 
Total Posts:  75
Joined:  2008-05-22
 

Scratch that!
I just altered design/frontend/--/--/layout/<module>.xml, since I saw that the <module>_index_index looked like a file structure.

Adam,
I’ve created another controller to give my module multiple pages, and thats working great. I now have IndexController and HomeController. Now I want to change the template being used from the default <Module>.phtml to home.phtml. I noticed that Block/<Module>.php sets the template, but creating Block/home.php and setting the new template there did not seem to work, and calling setTemplate() from HomeController, even with the scope resolution opperator, causes $this binding issues.

How can i change the template per page (IE: controller)

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
SwiftCol
Member
 
Total Posts:  75
Joined:  2008-05-22
 

Adam,
I have gotten multiple pages within a single block working great using your method here:

alistek - 02 July 2008 09:46 PM

One might logically think that you would go to:

www.example.com/index.php/news/adminhtml_news/view/

In reality you want to go to:
www.example.com/index.php/news/adminhtml_news/index/view/

Now something that I am working on figuring out, is having different sections of my module. I’m not sure if they should be called blocks or not. For instance, if you look at Magento’s Catalog module, there is admin/catalog_products/, which has all of it’s own tabs, grid, and edit pages than lets say admin/catalog_category/.

I would like to setup something like that with my module, but i’m having two problems:
1) I assume that I would need to copy the files in ModuleName/Block/Adminhtml/ModuleName/Edit/ to some where else, but i’m not sure what to call it, nor at what folder level I need to copy it. (At ModuleName or at Edit?)
2) Once i’ve copied the files correctly to have another section to my module, I don’t know how to access it from a URL. Your example above only triggers different actions inside one section of a module, where as I need to get to a new section. With normal modules this appears to be done with an underscore (admin/catalog_products/ vs admin/catalog_category/) but i can’t get anything like that to work in my module’s namespace.

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
50110 users|474 users currently online|102250 forum posts