I am using Laravel 6.0 and I try to list all my routes with artisan route:list
, but it fails and returns:
Illuminate\\Contracts\\Container\\
Now You Can use controller outside controller folder
use App\Http\submit;
Route::get('/', [submit::class, 'index']);
Now my controller excited in http folder
You have to change in controller file something
<?php
namespace App\Http;
use Illuminate\Http\Request;
use Illuminate\Http\Controllers\Controller;
class submit extends Controller {
public function index(Request $req) {
return $req;
}
}
For those who have similar issue with Illuminate\Contracts\Container\BindingResolutionException : Target class [<className>] does not exist.
message, this also could be helpful:
composer dump-autoload
In my case same error
occurred because of forward slash /
but it should be backward slash \
in defining route,
it happens when you have controller in folder
like as in my case controller was in api
Folder, so always use backward slash \
while mentioning controller name.
see example:
Error-prone code:
Route::apiResource('categories', 'api/CategoryController');
Route::apiResource('categories', 'api\CategoryController');
Run this command
php artisan config:cache
I was upgrading from Laravel 7 to Laravel 8 (Laravel 8 is still a few days in development) and also had this issue.
The solution was to use a classname representation of the controller in the route:
So in web.php instead of
Route::get('registration/create', 'RegistrationController@create')
it is now:
use App\Http\Controllers\RegistrationController;
Route::get('/', [RegistrationController::class, 'create']);
or as a string syntax (full namespaces controller name):
Route::get('/', 'App\Http\Controllers\RegistrationController@create');
As this should issue should only happen if you upgrade your application by creating a brand new laravel project you can also just add the default namespace to the RouteServiceProvider:
app/Providers/RouteServiceProvider.php
class RouteServiceProvider extends ServiceProvider
{
/* ... */
/** ADD THIS PROPERTY
* 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';
/**
* 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
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) // <-- ADD THIS
->group(base_path('routes/api.php'));
});
}
/* ... /*
}
See also https://laravel.com/docs/8.x/routing#basic-routing or https://laravel.com/docs/8.x/upgrade (search for "Routing").
I am running Laravel 8.x on my pc. This error gave me headache. To recreate the error, this is what I did: First I created a controller called MyModelController.php Secondly, I wrote a simple function to return a blade file containing 'Hello World', called myFunction. Lastly, I created a Route: Route::get('/','MyModelController@myFunction'); This did not work.
This was how I solved it. First you would have to read the documentation on: (https://laravel.com/docs/8.x/releases#laravel-8)
At the 'web.php' file this was the Route i wrote to make it work:
use App\Http\Controllers\MyModelController;
Route::get('/', [MyModelController::class, 'myFunction']);