路由文件:/routes/web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('lyget', function () {
return '路由GET方法';
});
Route::post('lypost', function () {
return '路由POST方法';
});
Route::match(['get','post'],'lymatch', function() {
return '路由Match方法';
});
Route::any('lyany',function(){
return '路由any方法';
});
Route::get('lygetcs1/{name}',function($name){
return $name;
});
Route::get('lygetcs2/{name}/{age}',function($cs1,$cs2){
return '我是'.$cs1.',今年'.$cs2.'岁了。';
});
Route::get('lygetcs3/{name?}/{age?}',function($name='',$age=''){
return '第一个参数:'.$name.';第二个参数:'.$age;
});
Route::get('lygetcs4/{name?}/{sex}/{age?}',function($name='',$sex,$age=''){
return '第一个参数:'.$name.';第二个参数:'.$sex.';第三个参数:'.$age;
});
Route::get('lypp1/{name}',function($name){
return '我叫'.$name;
})->where('name','[A-Z]+');
Route::get('lypp2/{name}/{age}',function($name,$age){
return '我叫'.$name.';今年'.$age;
})->where('name','[a-z]+')->where('age','[0-2]+');
Route::get('lypp3/{name}/{age}',function($name,$age){
return '我叫'.$name.';今年'.$age;
})->where(['name'=>'[a-z]+','age'=>'[0-2]+']);
Route::get('lyqj1/{name}',function($name){
return '我叫:'.$name;
});
Route::get('lyqj2/{name}',function($name){
return '我叫:'.$name;
});
Route::prefix('admin')->group(function(){
Route::get('lyz1',function(){
return "我是路由组 one";
});
Route::get('lyz2',function(){
return "我是路由组 two";
});
Route::get('lyz3',function(){
return "我是路由组 three";
});
});
Route::get('lym1',function(){
return "我是通过路由名访问的";
})->name('lym');
Route::get('lym2',function(){
return redirect()->route('lym');
});
Route::name('admin.')->group(function(){
Route::get('lymqz1',function(){
return "我是路由名前缀 one";
})->name('lyms1');
Route::get('lymqz2',function(){
return "我是路由名前缀 two";
})->name('lyms2');
Route::get('lymqz3',function(){
return "我是路由名前缀 three";
})->name('lyms3');
});
Route::get('lymcc',function(){
return redirect()->route('admin.lyms2');
});
Route::domain('baidu.com')->group(function(){
Route::get('lyym1',function(){
return "路由域名检测";
});
});
Route::group(['prefix'=>'admin','domain'=>'blog.com'],function(){
Route::get('lysz',function(){
return "数组方式检测";
});
});
Route::middleware('test')->group(function(){
Route::get('lyzjj',function(){
return "路由中间件";
});
});
中间件文件:app/Http/Middleware/Test.php
<?php
namespace App\Http\Middleware;
use Closure;
class Test
{
public function handle($request, Closure $next)
{
echo 'Test 路由中间件检测点';
return $next($request);
}
}
注册中间件文件:app/Http/Kernel.php
注意:这边定义的 test 名称与使用中间件名称一致
|