Home > Article > PHP Framework > Which directory is the laravel routing file in?
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.
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'; })
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');
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')); }
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 To match multiple request types, you can use the match method.
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
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>
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('/here', '/there', 301);
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('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
[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!