Target class [AdminController] does not exist - php

I have this error when a user returns from login to the admin page (i.e http://127.0.0.1:8000/admin), it should throw a 403 error if he/she doesn't sign in as the admin (i.e if he is not the admin)
Admin also experience this same error
Here's My Route
Laravel Version: 9.24.0
Please anyone should help
Here are my codes on web.php
<?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');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::middleware(['auth','admin'])->name('admin.')->prefix('admin')->group(function() {
Route::get('/', [AdminController::class, 'index'])->name('index');
});
require __DIR__.'/auth.php';

in app\Providers\RouteServiceProvider.php
uncomment this line
protected $namespace = 'App\\Http\\Controllers';
then command do
php artisan optimize
// or
php artisan optimize:clear

Thank you everyone for your help
I have identified the problem, I didn't add this on my kernel
'admin' => \App\Http\Middleware\Admin::class

Related

Laravel keeps saying "Target class [NewsletterController] does not exist.", I've tried other stuff online, but nothing helped

I know there is a heck ton of links out there about Laravel not finding some class and stuff, but I really tried everything here and nothing works. I donĀ“t even know which code I should share here to help, so...I'll share my Newsletter controller and the web.php code
I'm using laravel 9.9 and php 8.1
web.php
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsletterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| 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');
});
Route::get('newsletter','NewsletterController#create');
Route::post('newsletter','NewsletterController#store');
Newsletter controler
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Newsletter;
class NewsLetterController extends Controller
{
public function create()
{
return view('newsletter');
}
public function store(Request $request)
{
if ( ! Newsletter::isSubscribed($request->email) )
{
Newsletter::subscribePending($request->email);
return redirect('newsletter')->with('success', 'Thanks For Subscribe');
}
return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');
}
}
The issue is that your class name has a capital L for Letter, while on the route it does not, so your web.php should look like this:
<?php
Route::get('newsletter', 'App\Http\Controllers\NewsLetterController#register');
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\NewsLetterController;
/*
|--------------------------------------------------------------------------
| 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');
});
Route::get('newsletter','NewsLetterController#create');
Route::post('newsletter','NewsLetterController#store');
Maybe I am not sure
I think you can't use this method to write route in laravel 9 ...because I am using larvel 8 and it is not working so better to use:
Route::get('/usernewsletter, [NewsLetterController::class, 'create']);
https://laravel.com/docs/9.x/routing

How to create multiple route with laravel 5.8

I'm using version 5.8 of laravel and I'm trying to initialize the routes. It is not possible to access the route created after the default route:
Route::get('/', function () {
return view('welcome');
});
I try to add a parameter in the second route:
Route::get('/page', ['as' => 'home', function (/page) {return view('page1') ;}]);
I got 404 error. How can I use multiple routes ?
This is my web.php file:
<?php
/*
|--------------------------------------------------------------------------
| 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('/page', function() { return view('page1'); })->name('home');
Route::get('/', function () {
return view('welcome');
});
Did you copied the second route, or what are you trying to do with this?
You can try this one instead as your syntax is wrong:
Route::get('/page', function() { return view('page1'); })->name('home');
-- EDIT
You can also return just the views from your routes if you don't plan to use a controller, like this:
Route::view('/', 'welcome');
Route::view('/page', 'page1');

Unable to prepare route [api/user] for serialization. Uses Closure

I'm writing my project on Laravel. When I optimize the project, I have a problem :
Unable to prepare route [api/user] for serialization. Uses Closure.
I looked for any closures in web.php, but I didn't find anything
<?php
/*
|--------------------------------------------------------------------------
| 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('/','ReviewsController#main')->name('main');
Route::post('/','MailController#verify')->name('verifyPost');
Route::get('/reviews', 'ReviewsController#index')->name('reviews');
Route::post('/reviews','ReviewsController#add')->name('addReview');
Auth::routes();
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
in api.php file search and comment this route you will not get error..
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
and also in web.php file route::group is also closure and also comment them for test
Route::group(['middleware' => 'admin','prefix' => 'admin'],function () {
Route::get('/', 'HomeController#index')->name('admin');
Route::get('/reviews', 'Admin\ReviewsController#get')->name('admin.reviews');
Route::get('/reviews/accepted/{id}','Admin\ReviewsController#accept')->where('id','\d+')->name('admin.accepted');
Route::delete('/reviews/delete','Admin\ReviewsController#delete')->name('reviews.delete');
});
see what is closure
Php routing cache command :
php artisan route:cache
if your application using controller based routes. It help for fast execution. But remember "Closure based routes cannot be cached"
So kindly convert your Closure routes to controller classes.
For more information
Make sure to Check "routes/api.php"

New Laravel Routes not working

I have a problem, new routes in laravel are not working, url shows the correct route but almost as if it does not get to my routes web file just returns page not found every time.
I have tried:
using named route,
moving function to different controller,
clearing route cache,
clearing app cache,
dump-auto load,
made sure that AllowOverride is set to All,
Web.php:
<?php
/*
|--------------------------------------------------------------------------
| 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#index')->name('home');
/*
|--------------------------------------------------------------------------
| Courses
|--------------------------------------------------------------------------
*/
Route::get('/courses', 'CourseController#index');
Route::get('/courses/create', 'CourseController#create');
Route::get('/courses/{course}', 'CourseController#show');
Route::get('/courses/{course}/edit', 'CourseController#edit');
Route::post('/courses', 'CourseController#store');
Route::patch('/courses/{course}', 'CourseController#update');
Route::delete('/courses/{course}', 'CourseController#destroy')->name('course-delete');
Route::get('/courses/statistics', 'CourseController#statistics');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/section/{section}', 'SectionController#show');
/*
|--------------------------------------------------------------------------
| First Aid
|--------------------------------------------------------------------------
*/
Route::get('/progress', 'UserProgressController#index');
Route::get('/progress/create', 'UserProgressController#create');
Route::get('/progress/{section}', 'UserProgressController#show');
Route::get('/progress/formativeresults', 'UserProgressController#formativeresults');
//Route::get('/progress/coursestatistics', 'UserProgressController#coursestatistics');
//Route::get('/progress/{progress}/edit', 'UserProgressController#edit');
Route::post('/progress', 'UserProgressController#store');
//Route::patch('/progress/{progress}', 'UserProgressController#update');
//Route::delete('/progress/{progress}', 'UserProgressController#destroy')->name('progress-delete');
Controller:
public function statistics()
{
dd('Test');
return view('coursestatistics');
}
View file name:
coursestatistics.blade.php file structure views/coursestatistics
Link to page:
<a class="navbar-brand" href="/courses/statistics">
{{ __('Statistics') }}
</a>
Can anyone tell me what might be causing route not to work?
Try placing
Route::get('/courses/statistics', 'CourseController#statistics');
below this particular line of route code
Route::get('/courses/create', 'CourseController#create');
The general rule of laravel routing is to place specific routes before wildcard routes that are related. Link here
I had same issue, Did all those magic with configs and nothing..
Solution: run: php artisan route:clear
If issue remains same After cache clear or laravel routing rule, use 'composer dump-autoload'
If in your controller you have Model::findorFail($id) and an object with that ID does not exist, it can also lead to this error (in Laravel 6)

Route::Auth not surrounded in Middleware

<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Laravel routes.php. So when I look at Auth examples for Laravel 5.2, I ALWAYS ALWAYS see the Route::auth(); and Route::get('/home', 'HomeController#index') encapsulated in { of the Route::group(['middleware' => ['web']], function () { thing. But whenever I use the commands php artisan make:auth all it does is create the one I showed above.
Any clues on why this does this? It works fine and all but I'm not 100% sure if it's functioning properly. But I can tell you that I can login and sign-up properly. Did they do any changes to Laravel?
the web middleware is now applied by default:
https://github.com/laravel/laravel/tree/master/app/Http
Also in Kernel.php
https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php

Categories