Why my custom Laravel Route is not working? - php

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

Related

Although I created the route correctly But when I try to show the page It gives me an error page not found

This is my function in MemberController
public function text(){
return "hello";
}
This is my route
Route::get('/text',[MemberController::class,'text']);
So,basically its unable to read new route that i created.
Thanks in advance
Hope you're using laravel 8 and above
follwoing should be your controller code (MemberController.php)
<?php
namespace App\Http\Controllers;
class MemberController extends Controller{
public function text(){
return "hello";
}
}
and this should be your route code (web.php)
<?php
use App\Http\Controllers\MemberController;
Route::get('/text',[MemberController::class,'text']);
Code you have provided is not enough, though you can check following things.
See if your routes aren't cached. Run php artisan route:clear in order to clear the cached routes.
Where have you defined the route? If it's in the web.php file then, your actual url should be: http://localhost:8000/text.
If it's in the api.php file then, your url should be: http://localhost:8000/api/text.

I am getting Target class [Uploadcontroller] does not exist in laravel 9 and I don't know how to solve this

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

Class App\Http\Controllers\homeController does not exist

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']);

Laravel 5: Controller subdirectory and Route Group with multiple Controller does not work

Its might be a bug in laravel but not sure, need your suggestions about to resolve this.
The issue is when you use more than one controller under route:group with controller subdirectory except one controller other would be 404s.
Here's my detail code review for senerio:
Routes
#routes.php
#Settings
Route::group(array('prefix' => 'setting'), function() {
#Index
Route::controller('/', 'Setting\IndexController',[
'getIndex'=>'index/setting'
]);
#company detail
Route::controller('company', 'Setting\CompanyController',[
'getInfo'=>'info/company',
'getEdit'=>'update/company'
]);
});
Controllers
IndexController.php
#/app/Http/Controllers/Setting/IndexController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class IndexController extends Controller {
public function getIndex(){
return "setting.index";
}
}
CompanyController.php
namespace App\Http\Controllers\Setting;
use App\Http\Controllers\Controller;
class CompanyController extends Controller {
public function getInfo(){
return ('setting.company.index');
}
public function getEdit(){
return ('setting.company.edit');
}
}
Currently its not working but when you comment one route::controller other will work fine and viceversa.
and also if remove one route:controller and add route like:
Route::get('/', array('as' => 'index/setting', 'uses' => 'Setting\IndexController#getIndex'));
than both will work fine.
But I need to use route:controller for all controllers under route:group.
So, if still there something missing to explain let me know, I will update further in depth.
Any help would be appreciable.

Laravel 5 controller Routing does not work

I am familiar with Laravel 4 routes, but I am experiencing some problem with Laravel 5.
I code route.php as:
Route::get('/','HomeController#index');
and my HomeController.php is the following:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('index');
}
}
The output page displays as:
Whoops,looks like something went wrong.
The Route annotation file seems to be perfect.
The same case occurs for folder routing too!!
Please help me out.
First of all you should follow the instructions from James Njuguna in a comment to your question. Withoug debugging we can only guess whats going wrong.
In your case, most likely your error is, that the line
return View::make('index');
is causing an exception, because class App\Http\Controllers\View is not found. In this file a namespace is used, so you have to reference the root namespace like:
return \View::make('index');
OR you use a helper function
return view('index');
This function is documentated at http://laravel.com/docs/5.0/helpers#miscellaneous
If that's still failing... maybe you don't have an index.php or index.blade.php in your resources/views folder.
All, what #shock_gone_wild and #JamesNjuguna said is true. The reason of an error occurring is that you do not use namespaces when you call View.
For testing you can simply return text from a controller like this:
public function index()
{
return 'test'
}
and when it returns a result you can see what was the reason for the error and than you can change it with view global function, like #JamesNjuguna said.
Try this
public function index()
{
return view('home');
}
In laravel 5 the view class is not illuminated using a capital letter at the beginning

Categories