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 "<a href=\"{$url}\">{$url}<a/><br />\n";
        }
    }
    public function getIndex() {
        echo '我是用户列表^_^';
    }
    public function getShow($name) {
        echo "欢迎你,{$name}";
    }
}

创建BlogController控制器:

复制代码 代码如下:

php artisan make:controller BlogController

修改文件laravel\app\Http\Controllers\BlogController.php:

复制代码 代码如下:

<?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粉,但是我也没有想黑其他框架的意思,大家有兴趣也可以用自己熟悉的框架来实现这个小例子,写了记得@我,语言不限。

以上所述就是本文的全部内容了,希望大家能够喜欢。

请您花一点时间将文章分享给您的朋友或者留下评论。我们将会由衷感谢您的支持!


« 
» 

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3