i'm using laravel 5 , in rutes.php i have this code :
Route::get('about',"homeController#about");
and in App\Http\Controllers\ i have file homeController.php that contains :
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
but it throws this error : Class App\Http\Controllers\homeController does not exist .
how can i fix it ?
here is structure of the project and controllers :
First, check if you write the name of the controller correctly.
If it is, there are 3 methods:
Route::get('/about', 'App\Http\Controllers\homeController#about');
Write all the paths where your controller there is.
Route::get('/about', [HomeController::class, 'about');
Go to file RouteServiceProvider.php in Providers folder
Search for //protected $namespace = 'App\\Http\\Controllers';
( You will find it in comment)
Remove // of comment and save.
with this method, you will able to use the name of the controller directly without
writing all the paths.
Change all
homeController
To
HomeController
For HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function welcome()
{
return view('welcome');
}
}
For web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'welcome']);
Its work for me
change your routing like below:
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'index'])->name('home');
for more info look at this page:
https://laravel.com/docs/8.x/routing
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller as BaseController;
class homeController extends BaseController{
public function about(){
return view::make('about');
}
}
Should works perfectly. Are you sure that name of file is homeController.php ?
I faced a similar issue with the Home controller, in my case I saw the route was configured as following:
Route::get('/home',"homeController#index");
when I changed the above code to the following then it worked.
Route::get('/home',"App\Http\Controllers\HomeController#about");
In your case first, check the spelling first, whether it should be HomeController or homeController.
You can change your route code to the following
so you can try to change the following code
Route::get('about',"HomeController#about");
to
Route::get('about', 'App\Http\Controllers\HomeController#about');
or
Route::get('/about', 'App\Http\Controllers\HomeController#about');
Hope this will work.
It can happen because of these two causes:
Typo Error
Laravel Cache
1) Type Error
For this check, please check your HomeController file name and the class name in that file. Both should be same with case sensitivity.
HomeController.php
class HomeController extends
2) Laravel Cache
Laravel stores file caches with previous configurations. To refresh the cache, do this commands in the command window and then try again
php artisan cache:clear
php artisan view:clear
php artisan optimize
Hope it solved!.
Yes. That is right because you are not giving right path to the action in the routes. Either you update core files for path or provide manually in the route. e.g
you have
Route::get('about',"homeController#about");
try this route
Route::get('/about', [App\Http\Controllers\homeController::class,'about']);
or you can type route as
Route::get('/about', 'App\Http\Controllers\homeController#about');
furthermore you can check if you have right code in the controller function.
You are trying Laravel 7.x and before routing schema. Refer to 8.x documentation.
New syntaxis using as [HomeController::class, 'index'] or you need to add namespace before Controller name like App\Http\Controllers\HomeController.
Update the path to your route by adding .
Route::get('/home', [\App\Http\Controllers\HomeController::class, 'index'])->name('home');
Please update your route
use App\Http\Controllers\HomeController;
Route::get('/about', [HomeController::class, 'about']);
Related
After data from a form is saved i wanted to get back to the admin page.
I checked the database and the new data was there but I got an error:
"Route [pages.admin] not defined."
My Admin Controller code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Models\Admin;
class AdminController extends Controller
public function store(Request $request)
{
// Validation code
// Saveing code
return redirect()->route('pages.admin')
->with('success', 'Admins created successfully.');
}
My Page Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controllerpublic
function admin(){
return view('pages.admin');
}
Routes:
Route::get('/admin', 'PagesController#admin');
Route::post('admin_form', 'AdminController#store');
Would appreciate the help.
I looked in online sources but it didn't help
You are confusing the name of a view with the name of a route. Your view has the name pages.admin because there is a admin.blade.php view in the pages folder within the views folder of your application.
For route('pages.admin') to work, you need to assign a name to a route. You may do this by using name() when defining your route.
Route::get('/admin', 'PagesController#admin')->name('pages.admin');
It is a good practise to always name routes. For example: it allows you to just change the url without having to worry about your redirects breaking, since they use the name that hasn't changed.
I found a video and changed my code in the controller to
return redirect('admin');
and it worked.
I am learning Laravel 9, and I got a problem Target class [Admin\DashboardController] does not exist. when using prefix routing. Is someone can help me to fix it?
this code on routes/web:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
this code on App\Http\Controllers\Admin\DashboardController:
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(Request $request){
return view('pages.admin.dashboard');
}
}
You've specified an incorrect namespace in your route. As per the error message:
Target class [Admin\DashboardController] does not exist
Laravel expects to find a DashboardController in the Admin namespace, however, you've defined your DashboardController with the namespace App\Http\Controllers\Admin.
Update the namespace on your route.
Route::prefix('admin')->namespace('App\Http\Controllers\Admin')->group(function(){
Route::get('/','DashboardController#index')->name('dashboard');
});
If you read the documentation, you will see that you must use another way:
Route::prefix('admin')->namespace('Admin')->group(function(){
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
});
web.php
use App\Http\Controllers\Uploadcontroller;
Route::post('/upload', [Uploadcontroller::class, 'upload']);
Uploadcontroller.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class Uploadcontroller extends Controller
{
public function upload(Request $request){
dd($request->file('image'));
}
}
These are my route and controller codes. So, when I hit upload it is showing Uploadcontroller does not exist.I am new to Laravel and I don't know what is wrong here.
use the command
php artisan route:clear
and the web.php file
add in top
use App\Http\Controllers\UploadController;
Note:
Check that the letters of the file name and class name are the same case
Your codes seems just fine.
But dont forget everytime you add a Route to the 'web.php' file you need to rebuild the routing in Laravel.
Try run this in CMD:
php artisan optimize
I am a noob in laravel but never faced this issue in my previous 1-2 projects. I don't know why my custom route is not working, while the welcome route working fine. This is first-time I am getting this error 404 page not found.
web.php
Route::any('/dashboard', 'Admin\AdminController#tempirelounge')->name('dashboard');
Controller
namespace App\Http\Controllers\Admin;
//use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function tempirelounge()
{
return view('admin/index');
}
}
And the weird thing is that when I am trying to do this,
Route::get('/dashboard', function () {
return view('welcome');
});
It is not working
but when I do this
Route::get('/', function () {
return view('welcome');
});
It is working fine.
Does anyone have any idea why this is not working?
your Controller namespace is App\Http\Controllers, and in route u use ... Controllers\Admin\AdminController#tempirelounge where is true?
If your controller is in Admin folder then use correct name space.
Replace
App\Http\Controllers
with
App\Http\Controllers\Admin
Namespace is wrong
Approach 1:
Route::any('/dashboard', 'App\Http\Controllers\AdminController#tempirelounge')->name('dashboard');
Approach 2:
In web.php
use App\Http\Controllers\AdminController
Route::any('/dashboard', 'AdminController#tempirelounge')->name('dashboard');
Try to clear the route cache php artisan route:clear
This is my Route:
Route::get('/hello', '#HomeController#index');
This is my HomeController
namespace App\Http\Controllers;
use app\Requests;
use Illuminate\Http\Requests;
use Spatie\Activitylog\Models\Activity;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller {
public function index() {
$lastActivity = Spatie\Activitylog\Models\Activity::all();
return view('activity'), compact('lastActivity'));
}
}
But I keep on getting an error message:
ReflectionException in Route.php line 280:
Class App\Http\Controllers\ does not exist
What can I do? Thanks.
At the first of controller you do not need to put #, its just for method of controller.
Route::get('/hello', 'HomeController#index');
You have an extra # in your method call.
'#HomeController#index'
should be
'HomeController#index'
Whenever errors of type ReflectionException occur, you should check the routes in the routes/api.php and routes/web.php files to correct them ok understand do it carefully next time.