I was trying to register user using the Laravel Auth. First I got error with App\User not found then I fix it with App\Models\User it works. I don't really know what with Laravel 8 becuase I never had a problem with user registration with previous version. Then I got this problem
BadMethodCallException
Method App\Http\Controllers\HomeController::home does not exist.
I don't really know which code to provide since I don't even touch the default code of the Authentication/HomeController.
But I did changed the namespace in RouteProvider
protected $namespace = 'App\Http\Controllers';
web.php
Route::get('/home', [HomeController::class,'home']);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
in your web.php top you need to import
use App\Http\Controllers\HomeController;
then u can use
Route::get('/home', [HomeController::class,'index']);
Or else
Route::get('/home', 'HomeController#index')->name('home');
use this here no need to import
Note - your error is showing u don't have home method in your controller so create home method or change the correct method by default it is index
Try Route::get('/home', [HomeController::class,'index']);
Related
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');
}
}
This error keeps showing up on the browser even though I had tried a few solutions to fix it.
This is the code for PagesController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
//
public function index()
{
return view("pages.index");
}
}
Code for routes/web.php:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PagesController;
/*
|--------------------------------------------------------------------------
| 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("/", [PagesController::class, 'index']);
Code for RouteServiceProvider.php:
<?php
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* The path to the "home" route for your application.
*
* #var string
*/
public const HOME = '/home';
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
//
parent::boot();
}
/**
* Define the routes for the application.
*
* #return void
*/
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
//
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* #return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "API" routes for the application.
*
* These routes are typically stateless.
*
* #return void
*/
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
}
Everything just seems working fine to me, I did add the use statement in my web.php but it still shows PagesController and the default namespace in my RouteServiceProvider.php is 'App\Http\Controllers'. The file structure is also correct the PagesController.php is under the Controllers folder. I also tried to use commands to fix it like php artisan config:cache and composer dump-autoload but it did not work. Does anyone detect the mistake that I am making here? Thanks in advance.
In the new version of laravel, it works like this
Add This code
use App\Http\Controllers\PagesController;
and use the route as
Route::get('/', [PagesController::class,'index']);
First check PagesController is not a resource controller. if PagesController is resource controller so you can add bellow code.
php artisan route:list
<?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');
// });
// And use resource route
Route::resource('/', PagesController::class);
use resource route don't added PagesController controller in top. and if not solve please send message me on https://devnote.in I will help you.
If Everything is in the correct folder, I think there is some problem with you route file. Use this statement and check if your Controller can be find:
Route::get('/', 'PagesController#index');
And if it does not work use this one:
Route::get('/', 'App\Http\Controllers\PagesController#index');
it seems that you are upgrading from a previous Laravel version, isn't it?
Check RouteServiceProvider.php from source:
https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php
And make sure to make a comparison to your entire directory with:
https://github.com/laravel/laravel/compare/7.x...master
You could try putting:
use App\Http\Controllers\Controller;
In your PagesController.php as it is now needed in Laravel 8 (https://laravel.com/docs/8.x/controllers#defining-controllers)
I had a similar issue and this is how I solved it.
I included the following code in PagesController:
use App\Http\Controllers\Controller;
Then included the following code in routes (web.php)
use App\Http\Controllers\PagesController;
Route::get('/', [PagesController::class,'index']);
Laravel 8 is not a namespace prefix for your route groups where your routes are loaded.
"In previous releases of Laravel, the RouteServiceProvider contained a $namespace property. This property's value would automatically be prefixed onto controller route definitions and calls to the action helper / URL::action method. In Laravel 8.x, this property is null by default. This means that no automatic namespace prefixing will be done by Laravel."
When the namespace prefix is not used, you must use the fully qualified class name for your controllers when referring to them in your ways.
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']); // or
Route::get('/users', 'App\Http\Controllers\UserController#index'); If
you prefer the old way: App\Providers\RouteServiceProvider:
public function boot() {
...
Route::prefix('api')
->middleware('api')
->namespace('App\Http\Controllers') // <---------
->group(base_path('routes/api.php'));
... }
Do this for any route groups you want a declared namespace for.
The $namespace property:
Release Notes refer to the $namespace property to be set on your RouteServiceProvider in the Release notes and commented in your RouteServiceProvider this does not have any effect on your routes. It is currently only for adding a namespace prefix for generating URLs to actions. So you can set this variable, but it does not add these namespace prefixes, you need to make sure that you are using this variable when adding namespace in the way groups.
information is now an update guide
Laravel 8.x Docs - Upgrade Guide - Routing
With what the Upgrade Guide is showing the important part is that you are defining a namespace on your routes groups. Setting the $namespace variable by itself only helps in generating URLs to actions.
Again, and I can't stress this enough, the important part is setting the namespace for the route groups, which they just happen to be doing by referencing the member variable $namespace directly in the example.
uncomment:
you can uncomment the protected $namespace member variable in the RouteServiceProvider to go back to the old way, as the route groups are setup to use this member variable for the namespace for the groups.
// protected $namespace = 'App\\Http\\Controllers';
The only reason uncommenting that would add the namespace prefix to the Controllers assigned to the routes is because the route groups are setup to use this variable as the namespace:
<?php
namespace App\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* #var string
*/
public const HOME = '/home';
/**
* The controller namespace for the application.
*
* When present, controller route declarations will automatically be prefixed with this namespace.
*
* #var string|null
*/
**protected $namespace = 'App\\Http\\Controllers';**
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* #return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by(optional($request->user())->id ?: $request->ip());
});
}
}
Hi everyone I have a problem with laravel 8.1.0.
At the moment of wanting to create the routes in the web.php file
with the following code I get an error stating that the controller does not exist
web.php file
Route::get('/home/','HomeController#index');
I get the error "Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\HomeController] does not exist."
I have seen in the laravel documentation that version 8 has different calls for the controllers than the previous versions, I have applied the changes suggested by the documentation, however everything remains the same
I have added the following code in the file routeserviceprovider.php in app\providers
protected $namespace = 'App\Http\Controllers';
->namespace($this->namespace) //inside $this-> routes (function ()
->namespace($this->namespace) //inside Route::prefix('api')
According to the laravel documentation this should work, however I keep getting the same error.
"Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\HomeController] does not exist."
I have tried adding use App\Http\Controllers; in web.php file, but I have the same error message
I have tried using Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); and Route::get('/home', [HomeController::class, 'index']); but I have the same message error
web.php complete file
<?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');
});
Auth::routes();
//Route::get('/home', [HomeController::class, 'index']);
Route::get('/persona/','PersonaController#index')->name('per','persona');
Auth::routes();
//Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/home/','HomeController#index');
routeservicesprovider.php complete file
<?php
namespace sisVentas\Providers;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* #var string
*/
public const HOME = '/home';
/**
* If specified, this namespace is automatically applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers'; //agregado
/**
* Define your route model bindings, pattern filters, etc.
*
* #return void
*/
public function boot()
{
$this->configureRateLimiting();
$this->routes(function () {
Route::middleware('web')
->namespace($this->namespace) //agregado
->group(base_path('routes/web.php'));
Route::prefix('api')
->middleware('api')
->namespace($this->namespace) //agregado
->group(base_path('routes/api.php'));
});
}
/**
* Configure the rate limiters for the application.
*
* #return void
*/
protected function configureRateLimiting()
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60);
});
}
}
My homecontroller file
<?php
namespace sisVentas\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
return view('home');
}
}
The problem is the same with all the routes that I try to access.
I have lost a whole day trying to repair the routes, but I get the error of not finding them, I would like to know how to do it, thank you very much for reading this far :)
Your namespace is wrong.The app is searching for this controller in App\Http\Controller but your controllers namespace is sisVentas\Http\Controllers.
Depending on your directory structure if your root folder is sisVentas
you should use
protected $namespace = 'sisVentas\Http\Controllers';
Or if your root is App you should change your controller and providers namespace to
namespace App\Http\Controllers;
not require such kind of function in routeservicesprovider.php
protected $namespace = 'App\Http\Controllers'; x
please try this definitely will work..
Route::get('/home','App\Http\Controllers\Frontend\homeController#index');
write complete path.
and no need write to call top lavel
I'm trying to implement the reset password function using the built-in function from Laravel 5.7 as i have defined my routes in my web.php. I tried running php artisan route:list , It gave me an exception
UPDATE
Sorry for the lack of information given. I have already ran the command php artisan make:auth previously and the Auth::routes() has already been defined in web.php I am trying to access function resets in ResetPasswords traits through my ResetPasswordControllerbut it gave an exception
Class App\Http\Controllers\ResetPasswordController does not exist
I am using the pre-defined controller that is located at App\Http\Controllers\Auth\ResetPasswor.php
ResetPasswordController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
class ResetPasswordController extends Controller
{
use ResetsPasswords;
public function reset(Request $request){
$reset = $this->reset($request);
}
/**
* Where to redirect users after resetting their password.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
web.php
Auth::routes();
Route::post('password/reset','ResetPasswordController#reset');
SOLUTION
I have figured out where did i do wrong i had to add a Auth\ in my routes
Route::post('password/reset','Auth\ResetPasswordController#reset');
I am using Laravel 5.3 and trying to implement authentication system. I used php artisan command make:auth to setup it. I edited the views according to my layout and redirecting it my dashboard page instead of home (set as default in setup). Now, when I am trying to logout it throwing me this error
NotFoundHttpException in RouteCollection.php line 161
My code in routes/web.php is:
Auth::routes();
Route::get('/pages/superadmin/dashboard', 'HomeController#index');
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('dashboard');
}
}
Auth/Login Controller.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/dashboard';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
I tried the solutions on this page: How to set laravel 5.3 logout redirect path? but it didn't work and showing these errors:
ReflectionException in Route.php line 339:
Class App\Http\Controllers\Auth\Request does not exist
I want to redirect it to login page which is in auth/ folder.
I finally solved this issue by adding this line in my LoginController.php
protected $redirectAfterLogout = 'auth/login';
and editing this file \vendor\laravel\framework\src\Illuminate\Foundation\Auth\AuthenticatesUsers.php
It will use the default '/', if you don't provide $redirectAfterLogout in this file. You can also find it on github. Link is at the end of the answer.
public function logout()
{
return redirect(property_exists($this, 'redirectAfterLogout') ? $this- >redirectAfterLogout : '/');
}
You can also check it here: https://github.com/laravel/framework/commit/aa1204448a0d89e2846cbc383ce487df6efd9fc8#diff-b72935cc9bfd1d3e8139fd163ae00bf5
Hope it helps someone.
Thank You
If you want to continue to use GET to logout
Route::get('logout', 'Auth\LoginController#logout');
or
Route::get('logout', '\App\Http\Controllers\Auth\LoginController#logout');
Tested in Laravel 5.4
The solution that I believe works the best is overriding the inherited "logout" method that gets called from inside the app/Http/Controllers/Auth/LoginController.php file. Do this by adding the following method to this class:
/**
* Log the user out of the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/login');
}
As the "LoginController" class inherits from Illuminate\Foundation\Auth\AuthenticatesUsers, you should safely be able to override this method (in the LoginController) WITHOUT editing the actual vendor file itself... Editing the AuthenticatesUsers file, or any vendor file would cause major headaches down the road if you ever wanted to upgrade...
The only additional step here is that you need to include the following line at the top of the LoginController class:
use Illuminate\Http\Request;
In Laravel 5.3 logout is http post instead of http get. You can logout via post request like Taylor Otwell do in auth scaffolding.
<a href="{{ url('/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
Redirecting logout action to login page means to me there is no page in the site available unless user is authenticated.
I am not a big fan of touching the vendor directory as it is suggested above. It is true, Laravel 5.4 vendor/laravel/framework/src/Illuminate/Foundation/Auth/AutenticateUser->logout() redirects to '/'. There is no parameter available to change it. So let's keep it that way and just add an 'auth protection' to the route '/' (home)
in /routes/web.php add
Route::get('/', function () { return view('home'); })->middleware('auth');
Isn't it the simplest way ?
Works on laravel 5.6
File => app/Http/Controllers/Auth/LoginController.php
use Illuminate\Http\Request;
class LoginController extends Controller
{
... ... ...
... ... ...
... ... ...
/**
* Log the user out of the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->flush();
$request->session()->regenerate();
return redirect('/login');
}
}
Thanks The Virtual Machinist for your answer.