laravel作为一种主流的web应用程序开发框架,可以帮助开发人员快速高效地创建web应用程序。其中,资源路由(resource routes)是laravel框架中的一个非常有用的功能,它可以帮助开发人员轻松地定义项目中所需的url路由,降低了开发难度,并简化了代码实现。在本文中,我们将研究laravel资源路由的使用方法,帮助开发人员了解它的工作原理,以及如何在项目中使用它。
一、什么是laravel资源路由(resource routes)?
在laravel框架中,资源路由(resource routes)是一种特殊的路由类型,它允许开发人员将url路由和控制器方法绑定在一起,这样开发人员就可以很方便地创建crud(create, read, update, delete)操作了。
在使用资源路由时,开发人员只需要在routes/web.php文件中定义一个路由,laravel就会为该路由自动生成7个基本的crud操作方法,以及适当的路由名称。这极大地简化了代码实现,减少了开发难度。总之,使用laravel资源路由可以大大提高开发效率。
二、资源路由的基本语法
laravel资源路由的基本语法如下所示:
route::resource('resource_name', 'resourcecontroller');
其中,'resource_name'代表资源名称,'resourcecontroller'代表控制器名称。
laravel会基于这个资源名称自动生成7个restful路由,它们分别对应于7个基本的crud操作,如下所示:
方法uri操作名称
get /resource_name index resource_name.index
get /resource_name/create create resource_name.create
post /resource_name store resource_name.store
get /resource_name/{resource_name} show resource_name.show
get /resource_name/{resource_name}/edit edit resource_name.edit
put/patch /resource_name/{resource_name} update resource_name.update
delete /resource_name/{resource_name} destroy resource_name.destroy
至此,我们了解了laravel资源路由的基本语法和7个基本的restful路由。但是,有时候在项目中,我们需要自定义路由名称,或者修改路由方法。下面,我们将详细讲解如何自定义laravel资源路由。
三、自定义laravel资源路由
在laravel中,我们可以通过修改资源参数来自定义资源路由。下面,我们以'articles'为例,介绍自定义laravel资源路由的三种方法。
自定义路由名称如果我们不想使用laravel默认的路由名称,可以使用'as'命令来自定义路由名称。如下所示:
route::resource('articles', 'articlecontroller', ['names' => [ 'create' => 'articles.build', 'edit' => 'articles.modify']]);
这里,我们定义了自定义路由名称'articles.build'和'articles.modify',它们分别对应于articles/create和articles/{id}/edit这两条路由。
自定义路由方法除了自定义路由名称外,我们还可以通过修改资源参数来自定义路由方法。如下所示:
route::resource('articles', 'articlecontroller', ['only' => [ 'index', 'show']]);
这里,我们只定义了'index'和'show'这两个路由方法,因此'laravel'会生成对应的'get /articles'和'get articles/{id}'两个路由,并且隐藏默认的路由名称。
自定义资源参数如果我们不想使用'laravel'默认的资源参数'id',可以使用'parameters'命令来自定义资源参数。如下所示:
route::resource('articles', 'articlecontroller', ['parameters' => [ 'articles' => 'post']]);
这里,我们将资源名称'articles'修改为'post',这样'laravel'会接收到类似于'post /post'这种请求,并将'id'参数绑定到控制器方法中。
四、laravel资源路由实战
在本节中,我们将使用laravel资源路由来创建一个简单的在线笔记应用程序。首先,在routes/web.php文件中定义资源路由,如下所示:
route::resource('notes', 'notecontroller');
接下来,我们创建一个notecontroller,定义资源路由中7个基本的restful路由的实现方法。如下所示:
class notecontroller extends controller{ // 获取笔记列表 public function index() { // 获取所有笔记记录 $notes = note::all(); // 返回笔记记录列表视图 return view('notes.index', compact('notes')); } // 显示笔记创建视图 public function create() { // 返回笔记创建视图 return view('notes.create'); } // 创建新笔记 public function store(request $request) { // 数据验证 $request->validate([ 'title' => 'required|max:255', 'content' => 'required', ]); // 创建新笔记并保存到数据库 $note = new note(); $note->title = $request->input('title'); $note->content = $request->input('content'); $note->save(); // 重定向到笔记列表页面 return redirect('/notes'); } // 获取指定笔记详情 public function show(note $note) { // 返回指定笔记记录视图 return view('notes.show', compact('note')); } // 显示笔记编辑视图 public function edit(note $note) { // 返回笔记编辑视图 return view('notes.edit', compact('note')); } // 更新指定笔记 public function update(request $request, note $note) { // 数据验证 $request->validate([ 'title' => 'required|max:255', 'content' => 'required', ]); // 更新指定笔记并保存到数据库 $note->title = $request->input('title'); $note->content = $request->input('content'); $note->save(); // 重定向到笔记列表页面 return redirect('/notes'); } // 删除指定笔记 public function destroy(note $note) { // 删除指定笔记记录 $note->delete(); // 重定向到笔记列表页面 return redirect('/notes'); }}
在notecontroller中,我们实现了7个基本的crud操作方法,分别对应于7个资源路由。其中,我们使用了laravel自带的表单验证来验证用户输入的数据,以确保数据的准确性和完整性。
最后,在resources/views目录中创建7个视图文件,对应于7个基本的crud操作。如下所示:
resources/views/notes/index.blade.php:@extends('layouts.app')@section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <h1>laravel resource route demo</h1> <hr> <h2>note list:</h2> <ul> @foreach($notes as $note) <li><a href="/notes/{{$note->id}}>{{$note->title}}</a></li> @endforeach </ul> </div> </div> </div>@endsection
resources/views/notes/create.blade.php:@extends('layouts.app')@section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <h1>new note:</h1> <hr> <form method="post" action="/notes"> {{csrf_field()}} <div class="form-group"> <label for="title">title</label> <input type="text" class="form-control" name="title" id="title" placeholder="enter the title"> </div> <div class="form-group"> <label for="content">content</label> <textarea class="form-control" name="content" id="content" placeholder="enter the content"></textarea> </div> <button type="submit" class="btn btn-primary">submit</button> </form> </div> </div> </div>@endsection
resources/views/notes/show.blade.php:@extends('layouts.app')@section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <h1>note detail:</h1> <hr> <h2>title:</h2> <p>{{$note->title}}</p> <h2>content:</h2> <p>{{$note->content}}</p> <a href="/notes/{{$note->id}}/edit class=btn btn-primary>edit</a> <form method="post" action="/notes/{{$note->id}} style=display: inline-block;> {{csrf_field()}} {{method_field('delete')}} <button type="submit" class="btn btn-danger">delete</button> </form> </div> </div> </div>@endsection
resources/views/notes/edit.blade.php:@extends('layouts.app')@section('content') <div class="container"> <div class="row"> <div class="col-md-12"> <h1>edit note:</h1> <hr> <form method="post" action="/notes/{{$note->id}}> {{csrf_field()}} {{method_field('put')}} <div class="form-group"> <label for="title">title</label> <input type="text" class="form-control" name="title" id="title" value="{{$note->title}}> </div> <div class="form-group"> <label for="content">content</label> <textarea class="form-control" name="content" id="content">{{$note->content}}</textarea> </div> <button type="submit" class="btn btn-primary">update</button> </form> </div> </div> </div>@endsection
上面这四个视图文件分别对应于显示笔记列表、显示创建笔记表单、显示笔记详细信息和编辑笔记功能。
最后,我们运行服务器并访问http://localhost:8000/notes即可看到演示效果。
总结
本文我们介绍了laravel资源路由的基本用法和语法规则。我们从什么是laravel资源路由开始,深入到如何使用laravel资源路由创建crud工具,以及如何自定义laravel资源路由。最后,通过笔记应用程序的演示,加深了对于laravel资源路由的理解。现在,你掌握了使用laravel资源路由构建高效web应用程序的核心知识,可以应用到实际项目中了。
以上就是聊聊laravel中资源路由的使用方法的详细内容。