这个文件是骨架程序中自带的,我只是修改了router部分和controllers部分。要我写这么长的文件,那就太为难我了。这也是ZF官方发布了一个骨架程序的原因。
创建文件zf2\module\Application\src\Application\Controller\UserController.php:
<?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class UserController extends AbstractActionController { public function urlAction() { $name = 'jing'; $blogId = 1; $urls = array( $this->url()->fromRoute('user_list'), $this->url()->fromRoute('user_list/user', array('name' => $name)), $this->url()->fromRoute('user_list/user/blog_list', array('name' => $name)), $this->url()->fromRoute('user_list/user/blog_list/blog', array('name' => $name, 'blog_id' => $blogId)), ); $view = new ViewModel(compact('urls')); $view->setTerminal(true); return $view; } public function indexAction() { $view = new ViewModel(); // 禁用布局模板 $view->setTerminal(true); return $view; } public function showAction() { $username = $this->params()->fromRoute('name'); $view = new ViewModel(compact('username')); $view->setTerminal(true); return $view; } } ?>
创建文件zf2\module\Application\src\Application\Controller\BlogController.php:
<?php namespace Application\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class BlogController extends AbstractActionController { public function indexAction() { $username = $this->params()->fromRoute('name'); $view = new ViewModel(compact('username')); $view->setTerminal(true); return $view; } public function showAction() { $username = $this->params()->fromRoute('name'); $blogId = $this->params()->fromRoute('blog_id'); $view = new ViewModel(compact('username', 'blogId')); $view->setTerminal(true); return $view; } } ?>
zf2不支持Action参数绑定,ThinkPHP不仅支持绑定,还支持2种绑定方式:按变量名绑定和按变量顺序绑定。
zf2中Action必须得返回视图,除非exit()。如果你知道可以禁用视图的办法,请告诉我。
创建文件zf2\module\Application\view\application\user\url.phtml:
<?php foreach ($urls as $url): ?> <a href="<?php echo $url;?>"><?php echo $url;?><a/><br /> <?php endforeach; ?>
创建文件zf2\module\Application\view\application\user\index.phtml:
我是用户列表^_^
创建文件zf2\module\Application\view\application\user\show.phtml:
欢迎你,<?php echo $username; ?>
创建文件zf2\module\Application\view\application\blog\index.phtml:
这是<?php echo $username; ?>的博客列表
创建文件zf2\module\Application\view\application\blog\show.phtml:
复制代码 代码如下:
<?php echo $username; ?>的这篇博客的id为<?php echo $blogId; ?>
Yaf
安装Yaf
使用代码生成工具创建Yaf项目
修改启动文件yaf\application\Bootstrap.php,修改其中的_initRoute方法:
$router = Yaf_Dispatcher::getInstance()->getRouter(); $route0 = new Yaf_Route_Rewrite('url', array( 'controller' => 'User', 'action' => 'url', ), array() ); $route1 = new Yaf_Route_Rewrite('user', array( 'controller' => 'User', 'action' => 'index', ), array() ); $route2 = new Yaf_Route_Regex('#user/([a-zA-Z0-9_-]+)#', array( 'controller' => 'User', 'action' => 'show', ), array(1 => 'name',) ); $route3 = new Yaf_Route_Regex('#user/([a-zA-Z0-9_-]+)/blog#', array( 'controller' => 'Blog', 'action' => 'index', ), array(1 => 'name',) ); $route4 = new Yaf_Route_Regex('#user/([a-zA-Z0-9_-]+)/blog/([0-9]+)#', array( 'controller' => 'Blog', 'action' => 'show', ), array(1 => 'name', 2 => 'blogId',) ); $router->addRoute('url', $route0); $router->addRoute('user_list', $route1); $router->addRoute('user', $route2); $router->addRoute("blog_list", $route3); $router->addRoute("blog", $route4);
Yaf有路由功能,但是没有根据路由名生成URL的方法。所以我定义了一个项目名,用于拼接URL。
在配置文件中添加配置项yaf\conf\application.ini:
复制代码 代码如下:
project.name = 'yaf'
创建文件yaf\application\controllers\User.php:
<?php class UserController extends Yaf_Controller_Abstract { public function urlAction() { $name = 'jing'; $blogId = 1; $app = Yaf_Application::app(); $projectName = $app->getConfig()->project->name; $urls = array( "/{$projectName}/user", "/{$projectName}/user/{$name}", "/{$projectName}/user/{$name}/blog", "/{$projectName}/user/{$name}/blog/{$blogId}", ); foreach ($urls as $url) { echo "<a href=https://www.jb51.net/article/\"{$url}\">{$url}<a/><br />\n"; } return false; } public function indexAction() { echo '我是用户列表^_^'; // 禁用视图模板 return false; } public function showAction($name) { echo "欢迎你,{$name}"; return false; } }
创建文件yaf\application\controllers\Blog.php:
复制代码 代码如下: