laravel中的资源(resource)路由的使用方法

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:16   1494   0

laravel框架中的路由:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::match(['get', 'post'], $uri,$callback);
Route::any($uri, $callback);
Route::redirect('/here', '/there', 301);
Route::view('/welcome', 'welcome');                                                                                         Route::resource("/",'IndexController');

刚开始学习laravel框架的时候,只看到有resource路由,但是不知道该怎么用,研究了好久才明白是怎么回事。

首先,我用的是laravel5.5的框架,先在web.php中编写好路由,如:

Route::group(['prefix'=>'admin','namespace'=>'Admin'],function(){
 Route::resource("/",'IndexController');
});

这段代码其实很好理解。

prefix指的是访问的前缀,namespace则是访问的公共命名空间。

resource('域名/admin/',访问的控制器) 控制器中的方法

方法路径动作路由名称
GET/photosindexphotos.index
GET/photos/createcreatephotos.create
POST/photosstorephotos.store
GET/photos/{photo}showphotos.show
GET/photos/{photo}/editeditphotos.edit
PUT/PATCH/photos/{photo}updatephotos.update
DELETE/photos/{photo}destroyphotos.destroy

当输入的链接地址为:域名/admin/时,默认访问的是indexController中的index方法,而post提交的数据默认的是store方法

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP