dourang6423 2013-01-23 06:56
浏览 42
已采纳

Url不适用于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.

  • 写回答

1条回答 默认 最新

  • doujiejujixi27244 2013-01-23 10:17
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器