Laravel 5.3 NotFoundHttpException in RouteCollection.php - php

I have just installed laravel 5.3 and created a new controller. I have defined the routes but i am not getting to this url. I have written this code in routes/web.php file. Here is my routes code :
Routes file:
Route::resource('users','UsersController#getIndex');
Controller file:
namespace App\Http\Controllers;
class UsersController extends Controller
{
public function getIndex()
{
dd("test");
}
}

When you set routes like
Route::resource('users','UsersController#getIndex');
Routes will be created some thing like
App\Http\Controllers\UsersController#getIndex#store
App\Http\Controllers\UsersController#getIndex#index
App\Http\Controllers\UsersController#getIndex#create
and so on..
Change routes file as
Route::resource('users', 'UsersController');
And then rename controller method to
class UsersController extends Controller
{
public function Index()
{
dd("test");
}
}
It should work fine.

Try this one
Route::match(array('GET', 'POST'), 'users', 'UsersController#getIndex');
As, you will get access to only predefined methods through resource route.

Route::resource('users','UsersController');

Related

How can i access my controller located in "folder_name/controller_name" in codeigniter 4?

My Master controller located in "admin" folder. View image
here
namespace App\Controllers\admin;
use CodeIgniter\Controller;
class Master extends Controller
{
function __construct(){
helper('url');
}
public function index()
{
$data["content"]="view_home";
echo view('template/template', $data);
}
}
In my Routes.php i added this
$routes->get('admin/master','Master::index',['namespace','App\Controllers\admin']);
when i access the page in the browser i get this error
404 - File Not Found
Controller or its method is not found: {0}::{1}
What am i missing?
My silly mistake when setting up the route. I've put a "," instead of "=>". See the correct route below.
$routes->get('admin/master','Master::index',['namespace' => 'App\Controllers\admin']);

View not loading when called from a Controller in Laravel

I created a controller using artisan in my Laravel application, here's the code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class NavigationController extends Controller {
public function welcome() {
return view("welcome");
}
}
When I use a Closure or load the view directly, everything works fine. But, when I load view from inside a controller, it couldn't find it. Here's my web.php file's code:
Route::get('/', function () {
return view('NavigationController#welcome');
});
The error it shows:
InvalidArgumentException View [NavigationController#welcome] not found:
It's because the view NavigationController#welcome doesn't exist, this is a method.
Either you load the view from the closure :
Route::get('/', function() {
return view('welcome');
});
Either you call a method of the controller and this method loads the view:
Route::get('/', 'NavigationController#welcome');
Please see: Laravel Routing documentation

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

How to call controller function without declared in Route in Laravel 5.1?

i want to call the controller => function without specifying in route in Laravel 5.1.
such as controller / function
Example: admin/delete
so i want to call the above controller's function without specifying in routes is their any way to do that?
Also, if it is possible then how to pass parameters to that function?
I think you are looking for RESTful Controllers.
So in your route file you only have:
Route::controller('admin', 'AdminController');
and in your controller:
class AdminController extends BaseController {
public function show($adminId)
{
//
}
public function destroy($adminId)
{
//
}
}

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.

Categories