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 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决