ThinkPHP、ZF2、Yaf、Laravel框架路由大比拼
前言
读过一篇关于Zend Framework2的技术文章《ZF2多级树形路由Route配置实例》,是介绍路由配置的。我觉得很有意思,这是的需求:
/user对应用户列表页面
/user/:user_id对应用户的个人主页,比如 /user/AlloVince 就对应AlloVince用户的个人主页
/user/:user_id/blog/对应用户的博客列表页面,比如 /user/AlloVince/blog 就会列出AlloVince写过的Blog
/user/:user_id/blog/:blog_id对应用户的一篇博客文章
方案引用自原文:
'router' => array(
'routes' => array(
'user' => array(
'type' => 'Segment',
'options' => array(
'route' => '/user[/]',
'defaults' => array(
'controller' => 'UserController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'profile' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:id][/]',
'constraints' => array(
'id' => '[a-zA-Z0-9_-]+'
),
'defaults' => array(
'action' => 'get'
),
),
'may_terminate' => true,
'child_routes' => array(
'blog' => array(
'type' => 'Segment',
'options' => array(
'route' => 'blog[/]',
'constraints' => array(
),
'defaults' => array(
'action' => 'blog'
)
),
'may_terminate' => true,
'child_routes' => array(
'post' => array(
'type' => 'Segment',
'options' => array(
'route' => '[:post_id][/]',
'constraints' => array(
'post_id' => '[a-zA-Z0-9_-]+'
),
'defaults' => array(
'action' => 'post'
)
),
'may_terminate' => true,
),
),
),
), //profile child_routes end
), //profile end
), //user child_routes end
), //user end
),
),
看了这篇文章后,我打算使用我用过的PHP框架来实现这个路由需求。
ThinkPHP
新建一个ThinkPHP项目:
http://127.0.0.1/tp/url
输出:
'UserController@getUrl'));
Route::get('/user', array('uses' => 'UserController@getIndex'));
Route::get('/user/{username}', array('uses' => 'UserController@getShow'));
Route::get('/user/{username}/blog', array(
'as' => 'blog_list',
'uses' => 'BlogController@getIndex',
));
Route::get('/user/{username}/blog/{blogId}', array(
'as' => 'blog',
'uses' => 'BlogController@getShow',
))->where(array('blogId' => '[0-9]+'));
查看路由定义情况:
'UserController@getShow', array($name)),
route('blog_list', array($name)),
route('blog', array($name, $blogId)),
);
foreach ($urls as $url) {
echo "{$url}
\n";
}
}
public function getIndex() {
echo '我是用户列表^_^';
}
public function getShow($name) {
echo "欢迎你,{$name}";
}
}
创建BlogController控制器:
php artisan make:controller BlogController
修改文件laravel\app\Http\Controllers\BlogController.php:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class BlogController extends Controller {
public function getIndex($name) {
echo "这是{$name}的博客列表";
}
public function getShow($name, $blogId) {
echo "{$name}的这篇博客的id为{$blogId}";
}
}
Laravel的Action也支持参数绑定,是按变量顺序绑定的,和变量名无关。
后语
我是Laravel粉,但是我也没有想黑其他框架的意思,大家有兴趣也可以用自己熟悉的框架来实现这个小例子,写了记得@我,语言不限。
以上所述就是本文的全部内容了,希望大家能够喜欢。
请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!相关阅读:
Win7回收站消失了怎么恢复?Win7回收站图标恢复的两种方法
web前端开发JQuery常用实例代码片段(50个)
jquery ajax post提交数据乱码
php轻松实现中英文混排字符串截取
PHP实现服务器状态监控的方法
深入解析C++编程中的静态成员函数
C#中常量和只读变量的区别小结
Win10 9926如何使用全新的地图应用以便获取更多信息
Android StickListView实现悬停效果
IOS检测指定路径的文件是否存在
解析Android开发优化之:从代码角度进行优化的技巧
win8系统关机总是提示"Program Manager这个应用阻止关机"的解决方法
VS2010新建站点发布并访问步骤详解
PHP中抽象类和抽象方法概念与用法分析