【问题标题】:Initialization and using of routes in Yii (comparsion with Zend)Yii中路由的初始化和使用(与Zend比较)
【发布时间】:2012-09-12 13:13:14
【问题描述】:

我在 Zend 框架方面有一些经验。最近开始使用 Yii。

现在我试图在这个框架中找到一些类比。

在 Zend 中,几乎每条路由都有自己的名称。例如,您可以创建下一条路线“photos_map”:

$router->addRoute('photos_map',
    new Zend_Controller_Router_Route('map/:city', array(
        'controller' => 'photos', 
        'action' => 'map',
        'city' => ''
    ))
);

并在view url helper 中使用它:

echo $this->url(array(), 'photos_map') // output '/map'

在 Zend 中,您也可以在初始化或 url-helper 调用中传递参数(在上面的示例中为 city)。

如果您想更改 url,您可以将初始化 map/:city 中的参数字符串更改为您想要的任何内容。它非常有用,因为您不需要在代码中的任何地方将旧网址替换为新网址。

我的问题是这在 Yii 中可行吗?我流利地阅读了文档并开始认为 Yii 路由的功能要弱得多。这是牺牲性能还是我错过了什么?

【问题讨论】:

    标签: zend-framework yii


    【解决方案1】:

    与 Zend 相比,Yii 中的路由很简单,而且有点不同。在 Yii 中,视图是使用控制器渲染的,因此要渲染视图,您必须调用控制器。例如,您在一个站点的索引页面中,并且您想去预览页面。

    $url = Yii::app()->createUrl('/site/preview');
    //Here site is the name of the controller class and preview is the name of the action
    //You will need to have a controller named SiteController in your controllers folder
    //You will need to have a folder named "site" in your views folder
    //You will need to have an action(function) defined as actionPreview in your controller class
    

    现在在控制器类中,(在本例中为 SiteConroller.php),

    public function actionPreview()
    {
         $this->render('preview',array('data'=>''));
         //will render preview.php located in views/site/preview.php
         //u can pass parameters in array as shown above, in this case data 
    }
    

    如果你想改变 url,你可以简单地改变部分 $this->render('your_view_file.php'); 希望对您有所帮助.............欢迎提问......

    【讨论】:

    • 感谢您的回答。我今天花在了 Yii 的深度学习能力上,我很高兴我选择了这个框架,所以我的问题和任何疑问都消失了。我将通过将您的答案标记为已接受来结束此问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多