Skip to content
Advertisement

How to make URL from Laravel routes

I’ve got Laravel backend and I’m trying to make iOS to it, but there is no documentation. It is my first time with Laravel, so confused with the routes and middlewares. How do I compose URL from code below

Route::middleware('auth:api')->get('/user', function (Request $request) {
   return $request->user();
});

// Passport authentication
Route::group([
   'prefix' => 'auth'
], function () {
   Route::post('login', 'AuthController@login');
   Route::post('signup', 'AuthController@signup');

   Route::group([
     'middleware' => 'auth:api'
   ], function() {
       Route::get('logout', 'AuthController@logout');
       Route::get('user', 'AuthController@user');
       Route::resource('materials', 'MaterialsController');
       Route::resource('packages', 'PackagesController'); ```

Advertisement

Answer

The full path of your routes are the result of composition of some parts:

  • Base URL (domain, subdomain, IP)
  • Route prefix(es) (optional)
  • Route definition (basically, your endpoint)

Taking the following route defined in the web.php as an example:

// web.php
Route::get('my-cool-route', MyCoolController::class);

Then, that endpoint will have the following full path:

foobar.com/my-cool-route
           ^^^^^^^^^^^^^

The foobar.compart will be pulled out from the APP_URL key of your .env file

You could also have routes already prefixed (by you or by Laravel), for example the ones written inside the api.php file:

// api.php
Route::get('register', SignUpController::class);

By default, the routes inside api.php will be prefixed with api. So the route will look like this:

foobar.com/api/register
          ^^^^^^^^^^^^^

In case you wonder where this prefix is added, you can go to RouteServiceProvider.php to see how this is applied.

You can output the list of your routes with the help of Artisan.

php artisan route:list

Of course, this will throw ALL your site routes, so you could filter them by a lot of options like path por example:

php artisan route:list --path='materials'

That will list all the routes with a path that matches %materials%.

You can use the --help (or -h) option to see the full list of filter options.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement