tp5.0笔记1:url和路由

laravel技术交流群 :784030154

1.默认访问index/index/index

2.url访问时,其中控制器和操作名不区分大小写,针对控制器使用大驼峰命名方式,例如HelloWorld,则默认正确的访问url为index/hello_world/index,如将配置文件中的'url_convert'=>false,关闭url自动转换,则可使用index/HelloWorld/index方式访问。

3.参数传入:当控制器中的操作采用绑定参数转入,将自动获取url中的同名参数作为方法的参数,不受顺序的影响。

如将配置文件中的url_param_type=1,将严格按照转入的参数顺序获取。

ps:按照顺序绑定参数的话,操作方法的参数只能获取url pathinfo变量,将不接受get和post传值

4.隐藏index.php入口文件:

Apache:在入口文件的同级目录添加.htaccess文件

<IfModule mod_rewrite.c>
Options +FollowSymlinks -Multiviews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

Nginx :在Nginx.conf添加

location / { // …..省略部分代码
   if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php?s=/$1 last;
    break;
  }
}

5.隐藏url中模块名,则可在application/route.php配置路由

return [

    'hello/[:name]' => 'index/hello',  // 通过http://localhost/hello访问index/hello

   'hello/[:name]' => 'index/hello',    // 路由参数name为可选

   'hello/[:name]$' => 'index/hello',    // 必须以hello开头访问

    'hello/[:name]'=>['index/hello',['metod'=>'get','ext'=>'html']],//路由限制必须以get方式,.html后缀方式访问

    'blog/:year/:month' => ['blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'm
onth' => '\d{2}']],
    'blog/:id' => ['blog/get', ['method' => 'get'], ['id' => '\d+']],

    'blog/:name' => ['blog/read', ['method' => 'get'], ['name' => '\w+']],

    '__pattern__' => [
        'name' => '\w+',
        'id' => '\d+',
        'year' => '\d{4}',
        'month' => '\d{2}',
    ],//定义路由中的参数格式

];

设置url中的分隔符:application/config.php配置‘pathinfo_depr’=>'_'

ps:1.注意路由配置不支持在模块配置文件中设置。

        2.一旦设置了路由,就不能采用原有的pathinfo的index/hello/index访问

6.生成url地址

Url::build('blog/read','name=thinkphp');

Url::build('blog/read',['name'=>'thinkphp']);

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值