I am new in laravel 8, I add new Controller and edit web.php but still have this error:'Target class [App\Http\Controllers\App\Http\Controllers\PostController] does not exist'
this is PostController.php
<?php
namespace app\Http\Controllers;
class PostController extends Controller
{
public function index()
{
return view('articles');
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/','App\Http\Controllers\PostController@index' );
i also tried the other solution by adding namespace but doesnt work
$namespace = 'App\Http\Controllers';
it give me this error: "Class 'app\Http\Controllers\Controller' not found"
if anyone here can explain to me where is the problem I will be thankful
CodePudding user response:
just convert this to this code
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/','App\Http\Controllers\PostController@index' );
By this one :
enter code here
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
Route::get('/',[PostController::class, 'index']);
the error is you have written a laravel 7 , 6,etc code so the new routes in laravel 8 is like this [NameOfController::class,'name_of_method'] , you can take a look at laravel 8 docs https://laravel.com/docs/8.x/routing
CodePudding user response:
This is happing because default namespace is removed from laravel 8. You should enable from your RouteServiceProvider like below by uncommenting line
/**
* 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 = '/backend/dashboard';
/**
* 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 this