Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a REST API with Laravel Accessing Data with Resources Routes

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

Another breaking Change in Laravel 8

I've noticed another breaking change in Laravel 8.

I'm sure there's a way of starting a project in the version of Laravel that's used in this course but in case you're not, this helped me get back on track in the project I'm running with Laravel 8.


Defining Routes to controllers has changed in Laravel 8.

Per this link

https://laracasts.com/discuss/channels/laravel/target-class-carriercontroller-does-not-exist

Make the following changes to your RouteServiceProvider.php file in your Laravel 8 Project.

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * If specified, this namespace is automatically applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';  // make this change to the namespace variable.

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)   //add this line
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)  //add this line
                ->group(base_path('routes/api.php'));
        });
    }

    /**
     * Configure the rate limiters for the application.
     *
     * @return void
     */
    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60);
        });
    }
}

1 Answer

I fixed the routes issue "AuthorController" does not exist by adding these 2 lines to the api.php file:

use App\Http\Controllers\AuthorController; use App\Http\Controllers\BookController;