The most basic Laravel routes accept a URI and a closure, providing a very simple and expressive method of defining routes and behavior without complicated routing configuration files:<\/p>\n
1<\/span>use<\/span> Illuminate\\Support\\Facades\\<\/span>Route<\/span>;<\/span><\/div>2<\/span> <\/div>3<\/span>Route<\/span>::<\/span>get<\/span>(<\/span>'<\/span>\/greeting<\/span>'<\/span>, <\/span>function<\/span> <\/span>()<\/span> {<\/span><\/div>4<\/span> <\/span>return<\/span> <\/span>'<\/span>Hello World<\/span>'<\/span>;<\/span><\/div>5<\/span>});<\/span><\/div>\nuse Illuminate\\Support\\Facades\\Route;\n\nRoute::get('\/greeting', function () {\n return 'Hello World';\n});<\/div><\/code><\/pre>\n<\/div>\nThe Default Route Files<\/a><\/h3>\n
All Laravel routes are defined in your route files, which are located in the routes<\/code> directory. These files are automatically loaded by Laravel using the configuration specified in your application's bootstrap\/app.php<\/code> file. The routes\/web.php<\/code> file defines routes that are for your web interface. These routes are assigned the web<\/code> middleware group<\/a>, which provides features like session state and CSRF protection.<\/p>\n
For most applications, you will begin by defining routes in your routes\/web.php<\/code> file. The routes defined in routes\/web.php<\/code> may be accessed by entering the defined route's URL in your browser. For example, you may access the following route by navigating to http:\/\/example.com\/user<\/code> in your browser:<\/p>\n\n1<\/span>use<\/span> App\\Http\\Controllers\\<\/span>UserController<\/span>;<\/span><\/div>2<\/span> <\/div>3<\/span>Route<\/span>::<\/span>get<\/span>(<\/span>'<\/span>\/user<\/span>'<\/span>, [<\/span>UserController<\/span>::<\/span>class<\/span>, <\/span>'<\/span>index<\/span>'<\/span>]);<\/span><\/div>\nuse App\\Http\\Controllers\\UserController;\n\nRoute::get('\/user', [UserController::class, 'index']);<\/div><\/code><\/pre>\n<\/div>\nAPI Routes<\/a><\/h4>\nIf your application will also offer a stateless API, you may enable API routing using the install:api<\/code> Artisan command:<\/p>\n\n1<\/span>php <\/span>artisan<\/span> <\/span>install:api<\/span><\/div>\nphp artisan install:api<\/div><\/code><\/pre>\n<\/div>\nThe install:api<\/code> command installs Laravel Sanctum<\/a>, which provides a robust, yet simple API token authentication guard which can be used to authenticate third-party API consumers, SPAs, or mobile applications. In addition, the install:api<\/code> command creates the routes\/api.php<\/code> file:<\/p>\n\n1<\/span>Route<\/span>::<\/span>get<\/span>(<\/span>'<\/span>\/user<\/span>'<\/span>, <\/span>function<\/span> <\/span>(<\/span>Request<\/span> <\/span>$request<\/span>)<\/span> {<\/span><\/div>2<\/span> <\/span>return<\/span> <\/span>$request<\/span>-><\/span>user<\/span>();<\/span><\/div>3<\/span>})<\/span>-><\/span>middleware<\/span>(<\/span>'<\/span>auth:sanctum<\/span>'<\/span>);<\/span><\/div>\nRoute::get('\/user', function (Request $request) {\n return $request->user();\n})->middleware('auth:sanctum');<\/div><\/code><\/pre>\n<\/div>\nOf course, you are free to omit the auth:sanctum<\/code> middleware on routes that should be publicly accessible.<\/p>\nThe routes in routes\/api.php<\/code> are stateless and are assigned to the api<\/code> middleware group<\/a>. Additionally, the \/api<\/code> URI prefix is automatically applied to these routes, so you do not need to manually apply it to every route in the file. You may change the prefix by modifying your application's bootstrap\/app.php<\/code> file:<\/p>\n\n1<\/span>-><\/span>withRouting<\/span>(<\/span><\/div>2<\/span> api: <\/span>__DIR__<\/span>.<\/span>'<\/span>\/..\/routes\/api.php<\/span>'<\/span>,<\/span><\/div>3<\/span> apiPrefix: <\/span>'<\/span>api\/admin<\/span>'<\/span>,<\/span><\/div>4<\/span> <\/span>\/\/<\/span> ...<\/span><\/div>5<\/span>)<\/span><\/div>