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 do you think of laravel routing files

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about how to look at laravel routing files. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.

The laravel routing file is in the "routes" directory. All routing files in Laravel are defined in the routes directory, and its contents are automatically loaded by the framework; by default, there are four routing files in this directory for use by different entrances: web.php, api.php, console.php, etc.

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

It is convenient to simply define a route in Laravel by passing a URI and closure.

Route::get ('foo', function () {return' Hello World';})

All routes in Laravel are defined in the routes directory, and the contents of this directory are automatically loaded by the framework. By default, there are four route files in the routes directory for use by different entries: web.php, api.php, console.php, and channels.php.

The routes contained in the web.php file are within the web middleware group constraints defined by RouteServiceProvider, so Session, CSRF protection and Cookie encryption are supported. If the application does not need to provide stateless, RESTful-style API, then routes are basically defined in the web.php file.

The routes contained in api.php files are within the constraints of api middleware groups and support frequency restrictions. These routes are stateless, so requests to enter the application through these routes need to be authenticated by token and cannot access the Session status.

The console.php file is used to define all console commands based on closures, each of which is bound to a console command and allows interaction with command-line IO methods. Although this file does not define HTTP routing, it defines a console-based application entry (routing).

The channels.php file is used to register all event broadcast channels supported by the application.

Many projects are basically developed with routes/web.php as the starting point, which can meet the needs of rapid development of a project. Routes defined in routes/web.php are directly accessible in browsers. For example, enter the http://your-app.dev/user address in the browser to access the following route:

Route::get ('/ user', 'UsersController@index')

Routes defined in routes/api.php are nested in a routing group, which is set in RouteServiceProvider. The URI prefix / api is used for all routes in this group, so you don't have to add this prefix manually when you define routes. If you don't want to use the prefix / api, you can modify it in RouteServiceProvider.

/ * Define the "api" routes for the application. * * These routes are typically stateless. * * @ return void * / protected function mapApiRoutes () {Route::prefix ('api')-> middleware (' api')-> namespace ($this- > namespace)-> group (base_path ('routes/api.php'));}

Available routing methods

Each HTTP request type has a corresponding routing method available:

Route::get ($uri, $callback); Route::post ($uri, $callback); Route::put ($uri, $callback); Route::patch ($uri, $callback); Route::delete ($uri, $callback); Route::options ($uri, $callback)

Sometimes a route needs to be able to match multiple request types, and the match method can be used.

Route::match (['get',' post'],'/', function () {/ /})

If you want a route to match all request types, use the any method:

Route::any ('foo', function () {/ /})

CSRF protection

Using routes defined by Route::post, Route::put, Route::patch, and Route::delete, all HTML forms that access them are required to pass a CSRF token, otherwise the request will be rejected. You can read more about this in the CSRF documentation.

{{csrf_field ()}}...

Redirect routing

If you want to redirect one route to another, you can use the Route::redirect method. This saves some of the hassle of defining a complete route or controller to operate a simple redirection:

Route::redirect ('/ here','/ there', 301)

View routin

If you just want to simply return a view, you can use the Route::view method, which is similar to the Route::redirect method, and saves some trouble. The first parameter of the view method is URI, and the second parameter is the view name. Alternatively, you can use the optional third array type parameter to pass data to the view:

Route::view ('/ welcome', 'welcome'); Route::view (' / welcome', 'welcome', [' name' = > 'Taylor']). That's all of the article "what do you think of the laravel routing file?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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