View not loading when called from a Controller in Laravel - php

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

Related

Laravel: How to properly rewrite an authentification route in an MVC?

I have to setup my authentication for my reservations system in Laravel.
The basic routing methods by Laravel and a lot of tutorials is by writing the function for the route inside the web.php like this Laraval example:
Route::get('/flights', function () {
// Only authenticated users may access this route...
})->middleware('auth');
However, I was told this isn't the best way to set up routing and that I should use a controller to manage routes.
But that means I have to setup authentification methods inside the controller and to be honest, I don't have the vision to do this..
I thought I could do it like this (My Code)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Auth;
class PagesController extends Controller
{
public function home() {
return view('home');
}
public function reservations() {
return view('reservations')
->middleware(['auth']);
}
public function newreservations() {
return view('reservations_new');
}
}
combined with this web.php setup:
Route::get('/reservations.html', [PagesController::class, 'reservations']);
Route::get('/reservations.html/login', [AuthenticatedSessionController::class, 'create']);
Route::post('/reservations.html/login', [AuthenticatedSessionController::class, 'store']);
Route::get('/reservations_new.html', [PagesController::class, 'newReservations']);
but then I got the error:
Method Illuminate\View\View::middleware does not exist.
So any tips/tricks/methods to do it the right way?
There is no right or wrong way as long as you follow the same principal.
I personally do this:
Route::middleware('auth')->group(function () {
// Authenticated routes
});
you can check this extended discussion
also if you want to use middlewares in controller, this is the right way
class UserController extends Controller
{
/**
* Instantiate a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('log')->only('index');
$this->middleware('subscribed')->except('store');
}
}

Why my custom Laravel Route is not working?

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

Laravel 5.3 NotFoundHttpException in RouteCollection.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');

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.

Categories