'Laravel 8 Admin controller don't work - in what problem?
I have this route:
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['web', 'auth']], function () {;
Route::get('/', function () {
return view('backend.app');
})->middleware(['auth'])->name('dashboard');
Route::get('/news', 'News@index');
});
use App\Http\Controllers\News;
Route::get('/news', [News::class, 'index']);
Route::get('/news/{id}', [News::class, 'show']);
I need to open link /admin/news - but I have error: Target class [Admin\News] does not exist.
News class for admin:
<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
class News extends Controller
{
//
public function __construct()
{
}
public function index()
{
var_dump('test');
}
}
Can you help me: in what problem in my case?
Solution 1:[1]
You're mixing route definitions between the 'older' Laravel 7 style and the newer Laravel 8 style. Pick one and stick to it. If you've upgraded a project from Laravel 7 to Laravel 8, consider refactoring to keep things consistent.
As you have two News
controllers, you either want to use the FQN
(Fully Qualified Name) of each or alias one or both of your controllers.
use App\Http\Controllers\News;
use App\Http\Controllers\Admin\News as AdminNews; // aliased
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => ['web', 'auth']], function () {
Route::get('/', function () {
return view('backend.app');
})->middleware(['auth'])->name('dashboard');
Route::get('/news', [AdminNews::class, 'index']);
});
Route::get('/news', [News::class, 'index']);
Route::get('/news/{id}', [News::class, 'show']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Peppermintology |