定义路由规则后,原来的URL地址将会失效,变成非法请求。

定义路由
URL地址里面的index 模块怎么才能省略呢,默认的URL地址显得有点长,下面就来说说如何通过路由简化
URL访问。
我们在路由定义文件( application/route.php )里面添加一些路由规则,如下:
return [
// 添加路由规则 路由到 index控制器的hello操作方法
'hello/:name' => 'index/index/hello',
];
该路由规则表示所有hello 开头的并且带参数的访问都会路由到index 控制器的hello 操作方法。
路由之前的URL访问地址为:
http://tp5.com/index/index/hello/name/thinkphp
定义路由后就只能访问下面的URL地址
http://tp5.com/hello/thinkphp
注意
定义路由规则后,原来的URL地址将会失效,变成非法请求。

D:\LearnWebDevelop\php\thinkphp_5.0.5_full\application\route.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

return [
    // 添加路由规则 路由到 index控制器的hello操作方法
    'hello/:name/:city' => 'demo/index/hello',
//http://localhost/tp5/index.php/hello/jim/shanghai
//Hello,jim! You come from shanghai.

    '__pattern__' => [
        'name' => '\w+',
    ],
    '[hello]'     => [
        ':id'   => ['index/hello', ['method' => 'get'], ['id' => '\d+']],
        ':name' => ['index/hello', ['method' => 'post']],
    ],

];

D:\LearnWebDevelop\php\thinkphp_5.0.5_full\application\demo\controller\Index.php

    //localhost/tp5/index.php/demo/index/hello
    //http://localhost/tp5/index.php/demo/index/hello/city/shanghai/name/thinkphp
    //Hello,thinkphp! You come from shanghai.
    //http://localhost/tp5/index.php/demo/index/hello?city=shanghai&name=thinkphp111
    //Hello,thinkphp111! You come from shanghai.
    public function hello($name = 'World', $city = 'NewYork')
    {
        return 'Hello,' . $name . '! You come from ' . $city . '.';
    }

 

posted @ 2017-12-20 14:51  sky20080101  阅读(2106)  评论(0)    收藏  举报