Route::get('/', function () {
return view('welcome');
});
Route::any('/hello',function(){
return "Hello Laravel!";
});
Route::match(['get','post'],'/hello',function(){
return "Hello Laravel![get/post]";
});
Route::post('/hello',function(){
return "Hello Laravel[POST]!";
});
Route::get('/hello', function () {
return "Hello Laravel[GET]!";
});
Route::get('/hello/{name}',function($name){
//name必填
return "Hello {$name}!";
});
Route::get('/hello/{name?}',function($name="Laravel"){
//name选填
return "Hello {$name}!";
});
Route::get('/hello/{name?}',function($name="Larave2"){
//name只能是大小写字母
return "Hello {$name}!";
})->where('name','[A-Za-z]+');
//在全局范围内对参数进行条件约束,可以在app/Providers/RouteServiceProvider.php的boot方法中做如下定义:
public function boot(Router $router)
{
$router->pattern('name','[A-Za-z]+');
parent::boot($router);
}
路由命名
Route::get('/hello/laravelacademy',['as'=>'academy',function(){
return 'Hello LaravelAcademy!';
}]);
Route::get('/testNamedRoute',function(){
return redirect()->route('academy');
});
路由分组
Route::group(['as' => 'admin::'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
return "admin::dashboard";
}]);
});
Route::get('/testNamedRoute',function(){
return redirect()->route('admin::dashboard');
});