Magento Expert Forum - Improve your Magento experience

Results 1 to 5 of 5

How to create custom router?

  1. #1
    Junior Member golddev's Avatar
    Join Date
    Mar 2013
    Posts
    41
    Thanks
    1
    Thanked 9 Times in 5 Posts

    Post How to create custom router?

    Greeting! It’s time to share our knowledge on how to create custom router, as many of us use the standard router. You should know that Magento has four types of the routers: admin, standard, cms and default – they are loading in the order we’ve described. But, have you ever faced with such configuration?

    HTML Code:
    <routers>
        <atwixtest>
            <use>standard</use>
            <args>
                <module>Atwix_Test</module>
                <frontName>test-frontname</frontName>
            </args>
        </atwixtest>
    </routers>
    Yes, if you look to standard code, that means – Magento will use the standard router for processing requests and URLs, and when you follow magento.dev/test-frontname URL, in this case, Magento says for the standard router that it should take action indexAction from IndexController from module Atwix_Test – it is not so complicated, we guess. Actually, when the requests come to Magento, then it calls all routers till one of them will response – otherwise, there 404 page will be displayed.

    Now, let’s start creating own router. First of all, we need to declare router in config.xml:

    HTML Code:
    <default>
        <web>
            <routers>
                <atwixtest>
                    <area>frontend</area>
                    <class>Atwix_Test_Controller_Router</class>
                </atwixtest>
            </routers>
        </web>
    </default>
    After this, we should create file that will contain class Atwix_Test_Controller_Router app/code/local/Atwix/Test/Controller/Router.php:

    PHP Code:
    class Atwix_Test_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
    {
        public function 
    match(Zend_Controller_Request_Http $request)
        {
            
    $request->setModuleName('test-frontname')
                ->
    setControllerName('index')
                ->
    setActionName('index');
     
            return 
    true;
        }
     

    The previous code explains Magento that it should transmit control to the indexAction function which is placed in app/code/local/Atwix/Test/controllers/IndexController.php., and your router will be called after admin and standard, but if these routers do not respond for the requests – then your router will be called.
    So, now you can check request, proceed it and show page you want before Magento will throw “Not found” page. Hope that simple tutorial will be useful. We are waiting for your comments and happy, non-crappy coding!

    View more threads in the same category:


  2. #2
    New member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I've tried this steps, but always get the following Error

    Fatal error: Class 'DataLink_Mamut_Controller_Router' not found in /home/apache/public_html/example.com/includes/src/__default.php on line 17446

    app/etc/modules/DataLink_Mamut.xml:

    HTML Code:
    <?xml version="1.0"?>
    <config>
        <modules>
            <DataLink_Mamut>
                <active>true</active>
                <codePool>local</codePool>
            </DataLink_Mamut>
        </modules>
    </config>

    app/code/local/DataLink/Mamut/etc/config.xml

    HTML Code:
    <?xml version="1.0"?>
    <config>
    	<modules>
    		<DataLink_Mamut>
    			<version>0.0.1</version>
    		</DataLink_Mamut>
    	</modules>
    	<default>
    		<web>
    			<routers>
    				<datalink_mamut>
    					<area>frontend</area>
    					<class>DataLink_Mamut_Controller_Router</class>
    				</datalink_mamut>
    			</routers>
    		</web>
    	</default>
    </config>
    app/code/local/DataLink/Mamut/Controller/Router.php:

    PHP Code:
    <?php 

    class DataLink_Mamut_Controller_Router extends Mage_Core_Controller_Varien_Router_Standard
    {}
    Any idea about what can be happening here?

Similar Threads

  1. How to set a custom group of users
    By david in forum Programming & Development
    Replies: 9
    Last Post: 20-02-2021, 08:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •