March 1, 2012

routing of Zend framework

Question by PalAla

I create a module in zend project and the module has it’s own mvc folders, here’s the structure of the module,

enter image description here
i want to open the index page which located in the view floder of the visit module

here’s the path of the index.phtml

InspectionSysapplicationmodulesvisitsviewsscriptsvisitsindex.phtml

and I try to make routing to the index page in application.ini

resources.router.routes.user.route = /visit
resources.router.routes.user.defaults.module = visits
resources.router.routes.user.defaults.controller = visit
resources.router.routes.user.defaults.action = index

when I type http://localhost/zendApps/InspectionSys/visit it returns 404 error page.

What should I do?

Answer by Starx

Your controller’s name is visits not visit.

Try replacing your route with this

resources.router.routes.user.route = "/visit"
resources.router.routes.user.defaults.module = visits
resources.router.routes.user.defaults.controller = visits
resources.router.routes.user.defaults.action = index

or define your route in bootsrap

 $routeUser = new Zend_Controller_Router_Route(
    '/visit',
    array(
        'module' => 'visits'
        'controller' => 'visits',
        'action' => 'index'
    )
);
$router -> addRoute('visit', $routeUser);

Update 1

The problem seems to be due to the root not being routed to /public.

  1. The proper way: You need to setup a vhost and point the root to the public directory.

  2. Another Way: You need to redirect every request inside public directory. The .htaccess for this file would be

    RewriteRule ^.htaccess$ - [F]
    
    RewriteCond %{REQUEST_URI} =""
    RewriteRule ^.*$ /public/index.php [NC,L]
    
    RewriteCond %{REQUEST_URI} !^/public/.*$
    RewriteRule ^(.*)$ /public/$1
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^.*$ - [NC,L]
    
    RewriteRule ^public/.*$ /public/index.php [NC,L]
    

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!