25

I have a question that might sound silly to you so please forgive me.

I am not sure when do I use the routes/api.php file.

If I want to delete a record from a datatable with ajax, do I need to create a separate controller and put the route in api.php or can I use the same controller I use for everything else and put the route in web.php?

duncan
  • 31,401
  • 13
  • 78
  • 99
Sigal Zahavi
  • 1,045
  • 2
  • 21
  • 42

2 Answers2

26

I'm not sure if you read the Laravel documentation or how much familiar you are with Laravel, but in Laravel 5.3 you have web routes and api routes in separate files.

You use api routes only for registering your api (ie if you are building a rest api service), and all routes placed there will be prefixed by default with /api. So ie if you define a route /user inside the api file, it will be automatically prefixed with /api, so your end point would be www.yourapplication.com/api/user.

If you are not building a rest api service or anything similar dont use this file at all, use the web file for defining all of your application routes.

Also consider visiting Laracast website, as they have a nice introduction to new changes in Laravel 5.3 including web and api routes. Hope this helps you.

LeCodex
  • 1,636
  • 14
  • 20
bernadd
  • 660
  • 1
  • 10
  • 19
9

All that routes placed in api.php will be prefixed by /api, which was also mentioned by bernadd, there are other differences: in this link(https://mattstauffer.co/blog/routing-changes-in-laravel-5-3) you can find the difference between api and web in laravel code:

in App\Providers\RouteServiceProvider:

public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    protected function mapApiRoutes()
    {
        Route::group([
            'middleware' => ['api', 'auth:api'],
            'namespace' => $this->namespace,
            'prefix' => 'api',
        ], function ($router) {
            require base_path('routes/api.php');
        });
    }

    protected function mapWebRoutes()
    {
        Route::group([
            'namespace' => $this->namespace, 'middleware' => 'web',
        ], function ($router) {
            require base_path('routes/web.php');
        });
    }

in App\Http\Kernel.php in "protected $middlewareGroups" you can see this:

 'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

  'api' => [
            'throttle:60,1',
            'bindings',
        ],

And: in config\auth.php : In this file's Comments you can clearly find out the difference between default "auth"('guard' => 'web') vs "auth:api"

Hamid Asghari
  • 5,751
  • 4
  • 24
  • 34
Amirhossein72
  • 491
  • 6
  • 7