Yii中路由的初始化和使用(与Zend相比)



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

现在我试图在这些框架中找到一些相似之处。

在Zend,几乎每条路线都有自己的名字。例如,您可以创建下一条路线"photo_map":

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

url helper:在view中的应用

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

在Zend中,您还可以在初始化或url助手调用中传递参数(在上面的示例中,它是city)。

如果您想更改url,只需将初始化map/:city中的param字符串更改为您想要的。它非常有用,因为您不需要在代码中到处用新的url替换旧的url。

我的问题是,这在Yii中可能吗?我流利地阅读了文档,开始觉得Yii路线的功能要弱得多。这是对表演的牺牲还是我错过了什么?

Yii中的路由很简单,与Zend相比有点不同。在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');我希望它能有所帮助。。。。。。。。。。请随意提问。。。。。。

最新更新