search
HomeBackend DevelopmentPHP TutorialDetailed graphic and text explanation of laravel's routing (router)

This article mainly introduces the detailed graphic and text explanation of Laravel's routing (router), which has a certain reference value. Now I share it with everyone. Friends in need can refer to it

Detailed graphic explanation of laravel's routing (router)

Laravel basic routing:

In /routes/web.php, write a

Route::get('/hello',function(){
    return 'hello , can you hear me ?';
});

Then you can

Detailed graphic and text explanation of laravels routing (router)

postman can also see it directly in the browser

Detailed graphic and text explanation of laravels routing (router)

Original text:

Detailed graphic and text explanation of laravels routing (router)

Let’s try calling the controller first:

Route::get('/menu','Menu\MenuIndexController@index');

This is to directly get the request to send about, and call the about method of the StaticPagesController controller

Detailed graphic and text explanation of laravels routing (router)

<?php
namespace App\Http\Controllers\Menu;//修改命名空间
use App\Http\Controllers\Controller;//引用基础controller
use Illuminate\Http\Request;
class MenuIndexController extends Controller
{
    //
    public function index(){
        return view(&#39;menu/index&#39;);
    }
}

Jump to view:

Detailed graphic and text explanation of laravels routing (router)

@extends(&#39;layouts.default&#39;)
@section(&#39;content&#39;)
<h5 id="菜单页">菜单页</h5>
@stop
@section(&#39;title&#39;,&#39;菜单页&#39;)

Browser effect:

Detailed graphic and text explanation of laravels routing (router)

Defined in routes/ The routes in the api.php file are processed by app/Providers/RoutesServiceProvider and are nested in a routing middleware group. In this routing middleware group, all routes will be automatically added with the /api prefix, so you do not need to go to Manually add each route in the routing file. You can modify the routing prefix and other routing middleware group options by editing the RouteServiceProvider class;

Detailed graphic and text explanation of laravels routing (router)

Don’t bother with this one. Changed, I don’t know what magical things will happen if the underlying things are changed;

Sometimes you need to register a route to respond to multiple HTTP request actions - this can be achieved through the match method. Alternatively, you can use the any method to register a route to respond to all HTTP request actions:

Route::match([&#39;get&#39;,&#39;post&#39;],&#39;returnReturn&#39;,&#39;Menu\MenuIndexController@returnReturn&#39;);
Route::any(&#39;returnAny&#39;,&#39;Menu\MenuIndexController@returnAny&#39;);

Route redirection:

If you need to define a redirect to another URI route, you can use Route::redirect

Route::redirect(&#39;motherfucker&#39;,&#39;menu&#39;,301);

Enter motherfucker in the browser and you will jump to the menu;

301 is a status code, the default is 301, original text:

Detailed graphic and text explanation of laravels routing (router)

Of course There is also a need to jump directly to the view layer (view), and then the rest of the data (maybe whole data) is provided by the api. Then the route that jumps directly to the view is like this:

Route::view( 'staticView','static_pages/staticView');

Note that static_pages/staticView here uses forward slashes. Backslashes will cause an error saying can not found static_pages\staticView;

The static page is located at:

Detailed graphic and text explanation of laravels routing (router)

The effect of direct browser access:

Detailed graphic and text explanation of laravels routing (router)

Of course there is another kind of cool operation, that is Route::view passes the third parameter, which is used for data rendering in the view

Route::view(&#39;staticViewData&#39;,&#39;static_pages/staticViewData&#39;,[&#39;name&#39;=>&#39;jack&#39;,&#39;like&#39;=>&#39;money&#39;]);

The array passed is naturally ['name'=>'jack','like'=>'money' ],

Usage on the page:

@extends(&#39;layouts.default&#39;)
@section(&#39;content&#39;)
<h2 id="this-nbsp-is-nbsp-static-nbsp-view-nbsp-data">this is static view data</h2>
{{$name}} likes {{$like}}
 
@stop()
@section(&#39;title&#39;,&#39;static view data&#39;)

Then the browser effect:

Detailed graphic and text explanation of laravels routing (router)

Of course you want skin, then naturally you can’t :

Route::view(&#39;staticViewData&#39;,&#39;static_pages/staticViewData&#39;,[&#39;name&#39;=>&#39;jack&#39;,&#39;like&#39;=>&#39;money&#39;,&#39;jump&#39;=>&#39;<a href="/about">&#39;]);

The source code will parse the tags as ordinary text, and add

before and after. The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Recommended courses:

The latest laravel mall practical video tutorial

Comprehensive interpretation of the Laravel framework and practical video tutorial

Learn Laravel Easily-Basics

The above is the detailed content of Detailed graphic and text explanation of laravel's routing (router). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.