Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common routes in Laravel

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the common routes in Laravel". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "what are the common routes in Laravel?"

Laravel has six kinds of routes: 1, get, syntax "Route::get (..)"; 2, post, syntax "Route::post (..)"; 3, put, syntax "Route::put (..)"; 4, patch;5, delete;6, options.

The operating environment of this tutorial: windows7 system, Laravel6 version, DELL G3 computer.

What are the common routes in Laravel

To sum up in a word:

6 species: 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); 1. What are the CSRF protection form security measures for Lararel?

Purpose: 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 uses the authenticated user identity to run unauthorized commands.

Implementation: add the following sentence to the form: {{csrf_field ()}}. See the code for details

Essence: add a token value to the form form; input type= "hidden" name= "_ token" value= "SqlDhf... a random string"

The principle is: check whether the token value of the form is legal, pass it legally, otherwise it will not pass.

{{csrf_field ()}}.. .2. How to write the post request method in routing?

This is the post method: Route::post ('check', "LoginController@check")

3. How to implement put requests in routing?

Route writing: put method of Route: Route::put ('put','LoginController@put')

Controller writing: it is the basic controller writing.

Request page setup request type is put:input type= "hidden" name= "_ method" value= "put"

Write in routing

/ / put request Route::get ('putWeb','LoginController@putWeb'); / / put processing page Route::put (' put','LoginController@put')

Controller writing

/ / put request page public function putWeb () {return view ('putWeb');} / put request processing page public function put (Request $request) {dd ($request- > input ());}

Request page writing

{{csrf_field ()}} User:

4. How to achieve multiple requests on a Laravel page (I can get the page data, and the user can search the box to post the request)?

Match (specify request): Route::match (['get','post'],' /', "IndexController@index")

Controller code: judge whether the user is a post request, if so, output the user search, otherwise output the data that is normally displayed to the page

Any (all requests): Route::any ('/', "IndexController@index")

5. Examples and implementation of Laravel resource routing (more used in actual projects)?

Example: there are many modules in the background, such as user module, management module, etc. (add, delete, change, check): it is very troublesome to write directly in the route.

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 | # display Page | POST | Admin | Admin.store | App\ Http\ Controllers\ IndexController@store | web | # add Operation | GET | HEAD | Admin/create | Admin.create | App\ Http\ Controllers\ IndexController@create | web | # add Page | DELETE | Admin/ { Admin} | Admin.destroy | App\ Http\ Controllers\ IndexController@destroy | web | # Delete operation | PUT | PATCH | Admin/ {Admin} | Admin.update | App\ Http\ Controllers\ IndexController@update | web | # Update Operation | 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 | # modify page | GET | HEAD | api/user | | Closure | api Auth:api | 6. How to route with parameters and multiple parameters?

Parameter in curly braces: Route::get ('user/del/ {id}', function ($id) {echo $id;})

Multiple parameters: Route::get ('UserInfo/ {name} / {sex}', function ($name,$sex) {echo $name; echo $sex;})

Possible or impossible parameters: question mark + default value: Route::get ('user/edit/ {id?}', function ($id= "default value") {echo $id;});: with default value to guarantee a value without parameters

7. How to access the parameters in the routing in the controller?

Pass parameters directly. There is no value in $_ GET

Route::get ('UserInfo/ {name} / {sex}', "IndexController@UserInfo"); / / create a new UserInfopublic function UserInfo ($a) {var_dump ($a); var_dump ($sex1);} 8, what is a named route and what it does?

What is it: just name the route

Implementation: Route::get ('abc', "IndexController@abc")-> name (' one')

Function: get the url address of the alias: in the controller: route ('one'); you can go to the controller with the alias one

Function: redirect through named routing (a way to achieve redirection): in the controller: return redirect ()-> route ('one')

9. What is the function and implementation of the routing group namespace?

Implement the separation of front and background controllers: you need to create a new Admin directory under the controller directory and a new controller in the Home directory.

Specify the namespace in the group method of Route: Route::group (['namespace'= >' Admin'], function () {

The controller in Admin should write the namespace, and inherit the controller to use to the controller.

/ / routing code 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');}) # need to create a new Admin directory and Home directory under the controller directory and create a new controller in the directory # Note the code in the namespace controller needs to be modified

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report