Before we end this section there's a couple of things that we need to do.

  1. Refactor categories route

  2. Moving entire routes from web.php into api.php

  3. Remove unused files

1. Refactor categories route

Let's move the logic inside categories route declaration from web.php into a dedicated controller. Let's open up our terminal and type:

php artisan make:controller CategoryController -i

Note that I added -i option in the command above. It's a shorthand option of --invokable which tell artisan to generate an invokable controller class. If we open that file it would be like this:

class CategoryController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        //
    }
}

Now our categories route declaration will look as follow:

Route::get('/categories', 'CategoryController');

Note that we don't need to specify the action/method name in the second argument of get method since we've defined the __invoke method.

Our CategoryController will be look like this:

use App\Category;
use Illuminate\Http\Request;
use App\Http\Resources\CategoryResource;

class CategoryController extends Controller
{
    public function __invoke(Request $request)
    {
        $categories = Category::orderBy('name')->get();
        return CategoryResource::collection($categories);
    }
}

If we save the change and visit the categories URI in our browser or in Postman, we'll get the exact same thing.

2. Moving entire routes to api.php

As mentioned in the previous lessons that defining api routes would be better defined in api.php. So let's move entire routes except the root route from web.php into api.php file.

Now if we inspect our routes in our terminal through php artisan route:list command.

We'll see that all URI of our routes except the root route are prefixed with /api.

Also their middleware are now using api so that we don't need to worry about csrf token issue when sending POST, PUT or PATCH request.

For the products related routes we can simplified them by replacing them with apiResource like so:

If you inspect your routes, you'll get the exact same thing. The only difference is that they now have names which I think not really useful for external API use.

3. Remove unused files

Last, since we're building API for external use (we'll use this for our Products Management App), we no longer need any views inside products folder. So you might get rid of that folder.

CORS Stuff

Since we're using Laravel version 7 we almost don't need to setup CORS stuff in our app. In the prior version of Laravel we need to install laravel-cors package. But we don't need that because Laravel 7 has included that package by default.

You can see more information about CORS by hitting this link: Mozilla CORS documentation.

If you need to make some change, you can modify cors.php file in config folder.

<?php

return [

    'paths' => ['api/*'],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => false,

];