响应设置和重定向
1. 响应设置
- return 数组
return response()->json([1, 2, 3]);
- return 不想转换想直接返回出纯文本数据
return response()->view('index',['id'=>10])
->header('Content-type', 'text/plain');
return response('**index**')
->header('Content-type', 'text/plain');
2. 重定向
a. 最简单使用
return redirect('浏览路径名');
b. 方法二:
return redirect()->to('浏览器路径名');
c. 方法三:传入路由名的方法
return redirect()->route('路由名');
d. 方法四:返回到上一个路径
return redirect()->back();
e. 方法五:不管怎么跳转一定是跳转到away里面的路径
return redirect()->away('http://www.baidu.com');
资源控制器
1. 一般资源路由
a. 文件创建:
php artisan make:controller BlogController --resource
b. web.php操作单个资源路由
Route::resource('blogs', 'BlogController');
c. web.php操作多个资源路由
Route::resources([
'blogs' => 'BlogController'
]);
d. 限制可操作资源(仅可用)
Route::resource('blogs', 'BlogController')
->only(['index', 'show']);
e. 限制可操作资源(除了这些都可用)
Route::resource('blogs', 'BlogController')
->except(['index', 'show']);
2. api资源路由
a. 文件创建:
php artisan make:controller CommentController --api
b. 操作单个资源路由
Route::apiResource('blogs', 'BlogController');
其余的多个资源、限制操作参考一般资源路由方法
资源嵌套、浅嵌套、自定义
- 资源嵌套
Route::resource('浏览路由名1.浏览路由名2', "CommentController");
- 资源浅嵌套 (省略了单个需要传入的参数)
Route::resource('blogs.comments', "CommentController")
->shallow();
- 自定义路由
Route::resource('浏览路由名1.浏览路由名2', "CommentController")
->shallow()->name('index', 'b.c.i')->parameters([
'浏览路由名1' => '简化后名称1',
'浏览路由名2' => '简化后名称2'
]);
以上就是本次笔记分享啦