Here is my controller:
laravel 8 updated RouteServiceProvider and it affects route with string syntax, You can change it like above, but recommended way is using action syntax not using route with string syntax:
Route::get('register', 'Api\RegisterController@register');
Should be changed to:
Route::get('register', [RegisterController::class, 'register']);
If you would like to continue using the original auto-prefixed controller routing, you can simply set the value of the $namespace property within your RouteServiceProvider and update the route registrations within the boot method to use the $namespace property:
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
});
}
For solution just uncomment line 29:
**protected $namespace = 'App\\Http\\Controllers';**
in 'app\Providers\RouteServiceProvider.php' file.
just uncomment line 29
I had this error
(Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\ControllerFileName] does not exist.
Solution: just check your class Name, it should be the exact same of your file name.
ref link https://laravel.com/docs/8.x/upgrade
use App\Http\Controllers\SayhelloController;
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
or
Route::get('/users', 'App\Http\Controllers\UserController@index');
then in RouteServiceProvider.php
add this line
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers'; // need to add in Laravel 8
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // need to add in Laravel 8
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace) // need to add in Laravel 8
->group(base_path('routes/web.php'));
});
}
Then you can use like
Route::get('/users/{name?}' , [SayhelloController::class,'index']);
or
Route::get('/users', 'UserController@index');
In Laravel 8 the way routes are specified has changed:
Route::resource('homes', HomeController::class)->names('home.index');