laravel框架学习----course1--基本路由注册

(1)路由注册

1.可以在路由注册中直接渲染模板,使用Route::get('x.html',function(){return view('x')})

2.给单一控制器注册路由,在路由中只需要写类名就可以,不需要写方法名字,使用Route::get('unique.html','uniqueController');注意,单一控制器的方法名字需要使用 invoke()
3.给controllers下面的文件夹里面的控制器注册路由,使用Route::get('login.html','home\LoginController@login');
4.给views下面包含的文件夹里面的模板注册路由使用Route::get('adminindex.html',function(){
return view('admin.index');
});
5.给控制器里面渲染的方法注册路由,使用Route::get('first.html','类名@方法名');

(2) 路由注册的方法还可以使用any和match

Route::any('hello.html',function(){
return "hello !welcome";
});


Route::match(['get','post'],'b.html',function(){
return "hello !welcome";

});

(3)路由传参

路由传参主要分为必选参数和可选参数

下面是两个实例

// 必选参数
Route::get('index/{name}',function($name){
return $name.'同学你好!';
});
Route::get('index2/{name}/{number}',function($name,$number){
return $name.'同学你好!你的座位号是'.$number;
});
// 可选参数
Route::get('index3/{name?}',function($name='jia答应'){
return $name.'你今天怎么迟到了';

});注意:对于可选参数必须在function里面给一个默认值,在参数后面加问号代表是可选参数!!!

猜你喜欢

转载自blog.csdn.net/i_am_lonely/article/details/79963201
今日推荐