在ThinkPHP框架中,路由设置通常位于项目的application
目录下的route.php
文件中。以下是在Ubuntu系统中设置ThinkPHP路由的步骤:
打开终端,导航到你的ThinkPHP项目目录。
使用文本编辑器打开application/route.php
文件。例如,你可以使用nano编辑器:
nano application/route.php
route.php
文件中,你可以定义路由规则。以下是一些基本的路由示例:use think\Route;
Route::get('hello', 'index/Index/hello');
这将使得访问http://your-domain.com/hello
时,调用index
控制器的hello
方法。
Route::get('user/:id', 'index/User/read');
这将使得访问http://your-domain.com/user/1
时,调用index
控制器的read
方法,并将:id
参数传递给该方法。
Route::get('user/:id', 'index/User/read')->pattern(['id' => '\d+']);
这将使得访问http://your-domain.com/user/1
时,调用index
控制器的read
方法,并将:id
参数传递给该方法。同时,:id
参数必须是数字。
保存并关闭route.php
文件。
重启你的Web服务器以使更改生效。如果你使用的是Nginx和PHP-FPM,可以使用以下命令:
sudo service nginx restart
sudo service php7.x-fpm restart
将7.x
替换为你的PHP版本。
现在,你已经成功设置了ThinkPHP在Ubuntu系统中的路由。访问相应的URL,你应该能看到预期的结果。