Home > Article > PHP Framework > laravel has several routes
Laravel has 6 types of routes: 1. get, the syntax "Route::get($uri,$callback)"; 2. post, the syntax "Route::post($uri,$callback)"; 3. put, the syntax "Route::put($uri, $callback)"; 4. patch, the syntax "Route::patch($uri, $callback)"; 5. delete, the syntax "Route::delete($ uri, $callback)"; 6. options, syntax "Route::options($uri, $callback)".
The operating environment of this tutorial: Windows 7 system, Laravel 6 version, DELL G3 computer.
Summary in one sentence:
6 types: post, get, put, patch, delete, options
Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
Function: Laravel provides a simple way to protect your application from Cross-site request forgery (CSRF) attacks. Cross-site request forgery is a malicious attack that leverages the identity of an authenticated user to run unauthorized commands.
Implementation: Add the following sentence to the form: {{csrf_field()}}. See the code
for details. The essence is: adding a token value to the form; input type="hidden" name="_token" value="SqlDhf...a string of random characters String "
The principle is: Check whether the token value of the form is legal, if it is legal, it will pass, otherwise it will not pass
<form method="POST" action="/profile"> {{ csrf_field() }} ... </form>
is the post method: Route::post('check',"LoginController@check");
Route writing method: Route's put method: Route::put('put','LoginController@put');
Controller writing method: This is the writing method of the basic controller
The request page sets the request type to put: input type="hidden" name="_method" value="put"
Writing in routing
// put请求 Route::get('putWeb','LoginController@putWeb'); // put的处理页面 Route::put('put','LoginController@put');
Controller writing
// put请求页面 public function putWeb(){ return view('putWeb'); } // put请求处理页面 public function put(Request $request){ dd($request->input()); }
Request page writing
<form action="put" method="post"> <p> {{csrf_field()}} <input type="hidden" name="_method" value="put"> User: <input type="text" name="user" id=""> </p> </form>
match (specified request) :Route::match(['get','post'],'/',"IndexController@index ");
Controller code:Determine whether the user is making a post request, if so, output the user's search, otherwise output the data normally displayed on the page
any (all requests) :Route::any('/',"IndexController@index");
Example: There are many modules in the background, User module, management module, etc. (add, delete, modify, check): Write it directly in the routing A lot of trouble
Route::get('user/index','UserController@index'); Route::get('user/add','UserController@add'); Route::get('user/edit','UserController@edit'); Route::post('user/insert','UserController@insert'); Route::post('user/update','UserController@update'); Route::get('user/del','UserController@del');
Implementation: resource method: Route::resource('Admin','IndexController');
| GET|HEAD | Admin | Admin.index | App\Http\Controllers\IndexController@index | web | #展示页面 | POST | Admin | Admin.store | App\Http\Controllers\IndexController@store | web | #添加操作 | GET|HEAD | Admin/create | Admin.create | App\Http\Controllers\IndexController@create | web | #添加页面 | DELETE | Admin/{Admin} | Admin.destroy | App\Http\Controllers\IndexController@destroy | web | #删除操作 | PUT|PATCH | Admin/{Admin} | Admin.update | App\Http\Controllers\IndexController@update | web | #更新操作 | GET|HEAD | Admin/{Admin} | Admin.show | App\Http\Controllers\IndexController@show | web | | GET|HEAD | Admin/{Admin}/edit | Admin.edit | App\Http\Controllers\IndexController@edit | web | #修改页面 | GET|HEAD | api/user | | Closure | api,auth:api |
Parameters in braces: Route::get('user/del/{id}',function($ id){ echo $id##; });
Multiple parameters: Route::get('UserInfo/{name}/{sex}',function($name,$sex){ echo $name; echo $ sex; });
Parameters that may or may not be included:Question mark default value:Route::get('user/ edit/{id?}',function($id="default value"){ echo $id; }); : With a default value, it is guaranteed to have a value when there are no parameters.
Route::get('UserInfo/{name}/{sex}',"IndexController@UserInfo"); // 新建UserInfo public function UserInfo($a,$sex1){ var_dump($a); var_dump($sex1); }
是什么:就是给路由命名
实现:Route::get('abc',"IndexController@abc")->name('one');
作用:获取别名的url地址:控制器中:route('one');可以到以one为别名的控制器中
作用:通过命名路由实现重定向(实现重定向的一种方式):控制器中:return redirect()->route('one');
实现前后台控制器分离:需要在控制器目录下新建Admin目录和Home目录 在目录中新建控制器
在Route的group方法中指定命名空间:Route::group(['namespace'=>'Admin'],function(){
在Admin中的控制器要写好命名空间,并且继承控制器要use到控制器
//路由中代码 Route::group(['namespace'=>'Admin'],function(){ Route::get('admin','IndexController@index'); Route::get('admin/user','UserController@index'); Route::get('admin/goods','GoodsController@index'); }); Route::group(['namespace'=>'Home'],function(){ Route::get('/','IndexController@index'); }); #需要在控制器目录下新建Admin目录和Home目录 在目录中新建控制器 #注意 需要修改命名空间 控制器中代码 <?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use DB; use App\Http\Controllers\Controller; class UserController extends Controller { //index 方法 public function index(){ echo "我是后台控制器User"; } }
相关推荐:最新的五个Laravel视频教程
The above is the detailed content of laravel has several routes. For more information, please follow other related articles on the PHP Chinese website!