0
votes

Hi everyone I have a problem with laravel 8.1.0. At the moment of wanting to create the routes in the web.php file with the following code I get an error stating that the controller does not exist

web.php file

Route::get('/home/','HomeController@index');

I get the error "Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\HomeController] does not exist."

I have seen in the laravel documentation that version 8 has different calls for the controllers than the previous versions, I have applied the changes suggested by the documentation, however everything remains the same

I have added the following code in the file routeserviceprovider.php in app\providers

protected $namespace = 'App\Http\Controllers';
->namespace($this->namespace)  //inside $this-> routes (function ()
->namespace($this->namespace)  //inside Route::prefix('api')

According to the laravel documentation this should work, however I keep getting the same error. "Illuminate\Contracts\Container\BindingResolutionException Target class [App\Http\Controllers\HomeController] does not exist."

I have tried adding use App\Http\Controllers; in web.php file, but I have the same error message I have tried using Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); and Route::get('/home', [HomeController::class, 'index']); but I have the same message error

web.php complete file

<?php

use Illuminate\Support\Facades\Route; 


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});


Auth::routes();

//Route::get('/home', [HomeController::class, 'index']);
Route::get('/persona/','PersonaController@index')->name('per','persona');
Auth::routes();

//Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home/','HomeController@index');

routeservicesprovider.php complete file

<?php

namespace sisVentas\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';  //agregado

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

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

            Route::prefix('api')
                ->middleware('api')
                ->namespace($this->namespace)  //agregado
                ->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);
        });
    }
}

My homecontroller file

<?php

namespace sisVentas\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }
}

The problem is the same with all the routes that I try to access. I have lost a whole day trying to repair the routes, but I get the error of not finding them, I would like to know how to do it, thank you very much for reading this far :)

1
Can you show your HomeController? - aynber
May be a silly question, but does that controller actually exist? Does it have the correct namespace on top and is the namespace root correctly set in your composer.json ? - apokryfos
@aynber I have updated the post with the homecontroller included - André Cuellar Avaroma
Your namespace is sisVentas\Http\Controllers, not App\Http\Controllers - aynber
sisventas\http\controllers is still the wrong name. Note the name is case sensitive so it should be sisventas\Http\Controllers in addition can you share the segment called psr-4 in your composer.json in your root folder? - apokryfos

1 Answers

0
votes

Your namespace is wrong.The app is searching for this controller in App\Http\Controller but your controllers namespace is sisVentas\Http\Controllers.

Depending on your directory structure if your root folder is sisVentas you should use

    protected $namespace = 'sisVentas\Http\Controllers';

Or if your root is App you should change your controller and providers namespace to

    namespace App\Http\Controllers;