Home > PHP Framework > Laravel > body text

Which directory is the laravel routing file in?

青灯夜游
Release: 2022-04-28 20:58:36
Original
4817 people have browsed it

Laravel routing files are in the "routes" directory. All routing files in Laravel are defined in the routes directory, and the contents inside it will be automatically loaded by the framework; there are four default routing files in this directory for use by different entrances: web.php, api.php, console.php wait.

Which directory is the laravel routing file in?

The operating environment of this tutorial: Windows 7 system, Laravel 6 version, DELL G3 computer.

It is very convenient to simply define a route in Laravel, just pass a URI and closure.

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

All routes in Laravel are defined in the routes directory, and the content in this directory will be automatically loaded by the framework. There are four default routing files in the routes directory for use by different entrances: web.php, api.php, console.php and channels.php.

  • The routes contained in the web.php file are all within the constraints of the web middleware group defined by RouteServiceProvider, thus supporting Session, CSRF protection and Cookie encryption functions. If the application does not need to provide stateless , RESTful style API, then the routing is basically defined in the web.php file.

  • The routes contained in the api.php file are within the constraints of the api middleware group and support the frequency limiting function. These routes are stateless, so requests entering the application through these routes need to pass a token Authentication occurs and session state cannot be accessed.

  • The console.php file is used to define all closure-based console commands. Each closure is bound to a console command and allows interaction with command line IO methods. , although this file does not define HTTP routing, it does define the 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 using routes/web.php as the starting point, which can realize the need to quickly develop a project. Routes defined in routes/web.php can be directly accessed in the browser. For example, enter http://your-app.dev/user address in the browser to access the following route:

Route::get('/user', 'UsersController@index');
Copy after login

Routes defined in routes/api.php are nested in a routing group Here, this is set in RouteServiceProvider. The routes in this group all use the /api URI prefix, so you don't have to add this prefix manually when you define the route. If you don't want to use the /api prefix, 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'));
}
Copy after login

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);
Copy after login

Sometimes a route needs to be able to To match multiple request types, you can use the match method.

Route::match(['get', 'post'], '/', function () {
    //
});
Copy after login

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

Route::any('foo', function () {
    //
});
Copy after login

CSRF protection

Use Route::post, Route The routes defined by ::put, Route::patch and Route::delete require that the HTML form accessing them must pass a CSRF token, otherwise the request will be rejected. You can read more about this in the CSRF documentation.

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>
Copy after login

Redirect routing

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

Route::redirect(&#39;/here&#39;, &#39;/there&#39;, 301);
Copy after login

View Route

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

Route::view(&#39;/welcome&#39;, &#39;welcome&#39;);
Route::view(&#39;/welcome&#39;, &#39;welcome&#39;, [&#39;name&#39; => &#39;Taylor&#39;]);
Copy after login

[Related recommendations: laravel video tutorial]

The above is the detailed content of Which directory is the laravel routing file in?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template