Here is my controller:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class RegisterController extends Controller
{
public function register(Request $request)
{
dd('aa');
}
}
As seen in the screenshot, the class exists and is in the correct place:
My api.php
route:
Route::get('register', 'Api\RegisterController@register');
When I hit my register
route using Postman, it gave me the following error:
Target class [Api\RegisterController] does not exist.
Update:
Thanks to the answer, I was able to fix it. I decided to use the fully qualified class name for this route, but there are other options as described in the answer.
Route::get('register', 'App\Http\Controllers\Api\RegisterController@register');
Happened to me when I passing null to the middleware
function
Route::middleware(null)->group(function () {
Route::get('/some-path', [SomeController::class, 'search']);
});
Passing []
for no middleware works or probably just remove the middleware
call if not using middleware :D
Happened to me when I passing null to the middleware
function
Route::middleware(null)->group(function () {
Route::get('/some-path', [SomeController::class, 'search']);
});
Passing []
for no middleware works or probably just remove the middleware
call if not using middleware :D
In case if you prefer grouping of this routes, you can do as :
Route::group(['namespace' => 'App\Http\Controllers\Api'], function () {
Route::resource('user', 'UserController');
Route::resource('book', 'BookController');
});
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'));
});
}
use App\Http\Controllers\InvoiceController; // InvoiceController is controller name Route::get('invoice',[InvoiceController::class, 'index']);
protected $namespace = 'App\\Http\\Controllers';
In Laravel 8 the way routes are specified has changed:
Route::resource('homes', HomeController::class)->names('home.index');
The Laravel 8 documentation actually answers this issue more succinctly and clearly than any of the answers here:
In previous releases of Laravel, the RouteServiceProvider
contained a $namespace
property. This property's value would automatically be prefixed onto controller route definitions and calls to the action
helper / URL::action
method. In Laravel 8.x, this property is null
by default. This means that no automatic namespace prefixing will be done by Laravel. Therefore, in new Laravel 8.x applications, controller route definitions should be defined using standard PHP callable syntax:
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
Calls to the action
related methods should use the same callable syntax:
action([UserController::class, 'index']);
return Redirect::action([UserController::class, 'index']);
If you prefer Laravel 7.x style controller route prefixing, you may simply add the $namespace
property into your application's RouteServiceProvider
.
There's no explanation as to why Taylor Otwell added this maddening gotcha, but I presume he had his reasons.
For solution just uncomment line 29:
**protected $namespace = 'App\\Http\\Controllers';**
in 'app\Providers\RouteServiceProvider.php' file.
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.
CASE - 1
We can change in api.php
and in web.php
files like below..
The current way we write syntax
is
Route::get('login', 'LoginController@login');
should be changed to
Route::get('login', [LoginController::class, 'login']);
CASE - 2
In laravel-8 you can use like this
Route::group(['namespace'=>'App\Http\Controllers', 'prefix'=>'admin',
'as'=>'admin.','middleware'=>['auth:sanctum', 'verified']], function()
{
Route::resource('/dashboard', 'DashboardController')->only([
'index'
]);
});
Thanks
in laravel-8 default remove namespace prefix so you can set old way in laravel-7 like:
in RouteServiceProvider.php
add this variable
protected $namespace = 'App\Http\Controllers';
and update boot
method
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'));
});
}
The way to define your routes in laravel 8 is either
// Using PHP callable syntax...
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
OR
// Using string syntax...
Route::get('/', 'App\Http\Controllers\HomeController@index');
A resource route becomes
// Using PHP callable syntax...
use App\Http\Controllers\HomeController;
Route::resource('/', HomeController::class);
This means that in laravel 8, there is no automatic controller declaration prefixing by default.
If you want to stick to the old way, then you need to add a namespace property in the
app\Providers\RouteServiceProvider.php
and activate in the routes method.
Follow this image instructions below:
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']);
Just uncomment below line from RouteServiceProvider
(If does not exists then add)
protected $namespace = 'App\\Http\\Controllers';
If you are using laravel 8
just copy and paste my code
use App\Http\Controllers\UserController;
Route::get('/user', [UserController::class, 'index']);
On a freshly installed laravel 8, in the App/Providers/RouteServices.php
* 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';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* @var string|null
*/
// protected $namespace = 'App\\Http\\Controllers';
uncomment the
protected $namespace = 'App\Http\Controllers';
that should help you run laravel the old fashioned way.
Incase you are upgrading from lower versions of laravel to 8 then you might have to implicitly add the
protected $namespace = 'App\Http\Controllers';
in the RouteServices.php file for it to function the old way.
Source: Stackoverflow.com