Search code examples
phpzend-framework

Url not working for static page controller and router in Zend


I am doing web application using Zend framework. Library I am using is 1.12

In the project I have two modules i.e. Default and Admin

Default module have some static pages for that I have set router. In the router I have set controller and action, please refer below code.

In Bootstrap file,

    protected function _initModules() {

        $defaultstaticRoute = new Zend_Controller_Router_Route(
                                '/:staticpage',
                                array('module' => 'default',
                                    'controller' => 'index',
                                    'action' => 'displaystatic',
                                    'staticpage' => '([a-z0-9]+-)*[a-z0-9]+'  
                                    ),                 
                                array(
                                   'staticpage' => '([a-z0-9]+-)*[a-z0-9]+'                 
                                )
                            );

            $router->addRoute('defaultstatic', $defaultstaticRoute);
    }

In the controller,

class IndexController extends Zend_Controller_Action
{
    public function init()
    {

    }

    public function indexAction()
    {

    }

    public function displaystaticAction()
    {       
        //get the file name from the url
       $page = $this->getRequest()->getParam('staticpage');

        //render the view
       $this->render($page);

     }
}

The above code is working fine if the url is like http://myproject/index or http://myproject/aboutus

But, if the url without page name, like

http://myproject/

then it redirects me to 404 not found page while it have to show the index page.

I have tracked the issue, I found that it comes in the Index Controller's init() method and after it goes to 404.

What is the wrong in my code?

Edit:
The above issue is solved by the solution is given by Tim Fountain, But I found another issue and not solved by above trick. Below is the code,

In bootstrap file:

 $servicesstaticRoute = new Zend_Controller_Router_Route(
    'services/:pagename',
    array('module' => 'default',
            'controller' => 'services',
            'action' => 'displayservices',
            'pagename' => 'index'    
        ),                 
        array(
           'pagename' => '([a-z0-9]+-)*[a-z0-9]+'                 
        )
    );
$router->addRoute('servicesstatic', $servicesstaticRoute);

For the above code http://myproject/services/index is worked but http://myproject/services/ not worked.


Solution

  • The first array in your route provides default values. If you want http://myproject/ to render the static page 'index', you need to change the staticpage value in this array to 'index':

    $defaultstaticRoute = new Zend_Controller_Router_Route(
                                '/:staticpage',
                                array('module' => 'default',
                                    'controller' => 'index',
                                    'action' => 'displaystatic',
                                    'staticpage' => 'index'  
                                    ),                 
                                array(
                                   'staticpage' => '([a-z0-9]+-)*[a-z0-9]+'                 
                                )
                            );
    

    That way, if staticpage is not present in the URL it will be given the value 'index'.

    If you want the index action of your controller to be rendered instead, just remove the default value for staticpage and your route will no longer match this request.