路由相关笔记
Route::group
博客:中间件理解和误区
Route::group方法的意义大概就是能够同时给多个路由绑定同样的中间件、前缀、命名空间等。
如果路由过多,要给每一个路由都分配中间件、命名空间等太过麻烦。这时候就可以使用group方法对每一个路由的中间件。进行统一的分配。
app\Http\Controllers\Test\TestController.php
<?php
namespace App\Http\Controllers\Test;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function index(){
return 'index';
}
public function test(){
return 'test';
}
}
routes\web.php
use Illuminate\Support\Facades\Route;
Route::group(['prefix'=>'yaoxs','namespace'=>'Test'],function(){
// 注意:group所定义的命名空间、前缀、中间件是对方法里面定义的路由起作用(index、test).
Route::get('index', 'TestController@index');
Route::get('test', 'TestController@test');
});
调用地址: 127.0.0.1/yaoxs/index
执行结果: index
调用地址: 127.0.0.1/yaoxs/test
执行结果: test
使用name的好处
原文地址:www.cnblogs.com/redirect/p/8435823...
Route::get('/post/{post}', function () { // })->name('post.show');
要生成此路由的 URL,可以像这样使用辅助函数 route:
echo route('post.show', ['post' => $post]);
本作品采用《CC 协议》,转载必须注明作者和本文链接