ThinkPHP5的一些配置问题

url简化

隐藏url中的 index.php:

1、加载 mod_rewrite(Apache 配置文件 httpd.conf),去除'#'符号即可
2、继续在此,修改AllowOverride None的None更改为All。

3、在当前项目的public目录下,寻找.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>

具体可以看b站教程:https://www.bilibili.com/video/BV1L44y1s7UU/?vd_source=0a34e74118ab9b380e56298ab5319247

要注意目录的上下级,切勿操作错上级或下级文件,导致出现的错误

路由简化--省略url地址的index模块或其它模块

方法一:

在路由配置文件application/route.php 中添加如下规则

return [
    //添加路由规则,路由到 index控制器的hello操作方法
    'hello/:name' => 'index/index/hello',
    'helloW' => 'demo/TestC/helloW',
];
方法二:

采用动态定义路由规则的方式定义,在路由配置文件(application/route.php )的开头直接添加

use think\Route;
Route::rule('hello/:name', 'index/hello');

简化后的url访问:

http://tp5.com/hello/urName
http://tp5.com/helloW?name=thinkPHP

简化前的url访问:

http://tp5.com/index.php/模块/控制器/方法/传入的值

路由若需完整匹配

'hello/[:name]$' => 'index/hello',

路由规则以 $ 结尾的时候就表示当前路由规则需要完整匹配

定义闭包

定义闭包为某些特殊的场景定义路由规则,方法传入参数,因此url必须传入参数$name

return[
'hello/[:name]' =>function($name){
        return 'hello,'.$name.'!!!';
    }
];
    

或者

use think\Route;
Route::rule('hello/:name', function ($name) {
    return 'Hello,' . $name . '!';
});
posted @ 2023-03-29 18:01  亜光君  阅读(25)  评论(0)    收藏  举报