The problem that I'm having in my web app is that when a user types up a status and presses "add status" instead of it saving the status I get the following error : NotFoundHttpException in RouteCollection.php (line 179).
I have done some research online and looked at similar problems and tried changing it around but I still got the same problem back .
This is my Controller Class
<?php
namespace App\Http\Controllers;
use 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(Request $r)
{
if(Request::has('status-text'))
{
$text = e(Request::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->user_id = Auth::user()->id;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
This is my web.php class
<?php
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses'=>'HomeController#index']);
This is an image of the error that I'm getting back :
First thing is you can just have return view() in the routes/web.php file. All you should need to do is make your web.php like this:
Auth::routes();
Route::get('/home','HomeController#index');
that should fix your problem.
Related
I am studying multiple authentication.
In particular I have 3 users:
a User user who must be redirected to /home when logging in
an Admin user who must be redirected to /admin/home when logging in
a Manager user who must be redirected to /manager/home when logging in
The problem I am having is when I log in as Admin and as Manager I am redirected to the route /home and then I get the error
["You do not have permission to access for this page."]
However, once I log in, if I manually enter the route of interest I can log in without problems.
So the problem is the route addressing once I try to log in as Admin or as Manager.
For the User user I'm not having any problems.
This is my code:
Route.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
/*------------------------------------------
--------------------------------------------
All Normal Users Routes List
--------------------------------------------
--------------------------------------------*/
Route::middleware(['auth', 'user-access:user'])->group(function () {
Route::get('/home', [HomeController::class, 'index'])->name('home');
});
/*------------------------------------------
--------------------------------------------
All Admin Routes List
--------------------------------------------
--------------------------------------------*/
Route::middleware(['auth', 'user-access:admin'])->group(function () {
Route::get('/admin/home', [HomeController::class, 'adminHome'])->name('admin.home');
Route::get('/admin/link', [HomeController::class, 'adminHello'])->name('admin.hello');
});
/*------------------------------------------
--------------------------------------------
All Admin Routes List
--------------------------------------------
--------------------------------------------*/
Route::middleware(['auth', 'user-access:manager'])->group(function () {
Route::get('/manager/home', [HomeController::class, 'managerHome'])->name('manager.home');
});
LoginController
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 = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password'])))
{
if (auth()->user()->type == 'admin') {
return redirect()->route('admin.home');
}else if (auth()->user()->type == 'manager') {
return redirect()->route('manager.home');
}else{
return redirect()->route('home');
}
}else{
return redirect()->route('login')
->with('error','Email-Address And Password Are Wrong.');
}
}
}
HomeController
<?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');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function adminHome()
{
return view('adminHome');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function managerHome()
{
return view('managerHome');
}
}
UserAccess
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class UserAccess
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* #return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, $userType)
{
if(auth()->user()->type == $userType){
return $next($request);
}
return response()->json(['You do not have permission to access for this page.']);
/* return response()->view('errors.check-permission'); */
}
}
Can you kindly help me?
In most of my applications I have an admin panel.
Here's how I do the redirect logic:
I use the default Auth/AuthenticatedSessionController class from the breeze install.
My store method looks like this:
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
if (Auth::user()->hasRole('admin')) {
return redirect()->intended(RouteServiceProvider::ADMIN_HOME);
}
return redirect()->intended(RouteServiceProvider::HOME);
}
And of course in the RouteServiceProvider I hav my routes defined:
public const HOME = '/myorders';
public const ADMIN_HOME = '/admin/pages';
Solution 1:
On your App\Http\Controllers\Auth\LoginController, just override the method:
use Illuminate\Support\Facades\Auth;
public function redirectPath()
{
if (Auth::user()->role == 'Admin') {
return "/admin/home";
// or return route('admin.home');
}
elseif (Auth::user()->role == 'Manager') {
return "/manager/home";
// or return route('manager.home');
}
return "/home";
// or return route('home');
}
N.B: If something issue happenes with the method redirectPath, then please try with the method redirectTo. And must remove the property named redirectTo as well.
Solution 2:
App\Http\Controllers\Auth\LoginController.php
use Illuminate\Support\Facades\Auth;
protected function authenticated(Request $request, $user)
{
if (auth()->user()->hasRole(['Admin'])) {
return redirect("/admin/home");
}
elseif (auth()->user()->hasRole(['Manager'])) {
return redirect("/manager/home");
}
return redirect("/home");
}
N.B: If you are using Laravel Spatie Permission package, then the permission checking would work in this way.
I have this situation where I have a route /admin which requires Auth middleware to be activated. I have the middleware requirement indicated in the web.php route. Also, I do have the default auth setup of laravel.
The kernel.php does have the middleware indicated too.
But, weirdly enough /admin brings me to a white page with nothing. When logged in, the problem isn't there. It had been working and all of a sudden it was not working anymore.
The auth middleware is as it is:
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*
* #param \Illuminate\Http\Request $request
* #return string
*/
protected function redirectTo($request)
{
return route('login');
}
}
The controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\NewsletterSubscribers;
use App\User;
use File;
class adminController extends Controller
{
//
public function __construct()
{
$this->middleware('auth');
$this->middleware('admin');
}
public function index(){
return view('admin.home');
}
public function changebg(){
return view('admin.changebg');
}
public function changebgimage(Request $request){
$this->validate($request,
[
'image'=>'required|image|mimes:jpg,JPG,jpeg,JPEG|max:4096|dimensions:max_width:1600,max_height:1100',
]
);
$path="images/";
$imagepath="images/bg.jpg";
if( File::exists($imagepath))
{
unlink($imagepath);
}
if ( ! File::exists($path) )
{
File::makeDirectory($path,0777,true);
}
$getimageName = "bg.jpg";
$request->image->move(public_path($path), $getimageName);
return view('admin.home');
}
public function newslettersubscriberslist(){
$newslettersubscribers= NewsletterSubscribers::all();
$count=0;
return view('admin.subscriberslist',compact('newslettersubscribers','count'));
}
public function registerAdmin(){
return view('auth.adminregister');
}
public function viewAdmins(){
$admins= User::select('users.*')->where('role','=','admin')->get();
//print_r($admins);
$count=0;
return view('admin.adminlist',compact('admins','count'));
}
public function viewUsers(){
$users= User::select('users.*')->where('role','=','user')->get();
//print_r($admins);
$count=0;
return view('admin.userlist',compact('users','count'));
}
}
The admin middleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
class Admin
{
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next)
{
if (Auth::check() && Auth::user()->role == 'admin') {
return $next($request);
}
else {
return redirect('/login');
}
}
}
The the route I'm using:
Route::get('/admin', 'AdminController#index')->name('adminhome')->middleware('auth');
I dont find anything weird here but weirdly enough the problem exists. Can you guys trace somethin unusual here or somewhere it can be??
First of all, make sure you have error reporting turned on. Also take a look at laravel log. Looking at your code the problem might be case of AdminController. In routes you have 'AdminController#index' but the class you showed has name adminController and it should be AdminController. I also don't know what is the name of file but it should be again AdminController.php
You can use the middleware like this
Route::group(['middleware'=>'auth'], function()
{
Route::get('/admin', 'AdminController#index')->name('adminhome');
}
I'm new to Laravel so I'm not familiar with errors in the framework .I'm trying to get the user to make a post but I'm getting the above error .Could you please tell where I'm going wrong ?Thank you
This is my HomeController class:
<?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(Request $r)
{
if(Input::has('status-text'))
{
$text = e(Input::get('status-text'));
$userStatus = new Status();
$userStatus->status_text = $text;
$userStatus->save();
Flash::success('Your status has been posted');
return redirect(route('home'));
}
return view('home');
}
}
And this is my web.php class :
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::any('/home', ['as'=> 'home','uses' =>'HomeController#index']);
Don't use Input::get(), use $r->get() as you're injecting the request as a dependency to the index method already, and Input:: is merely a alias to access the underlaying Request.
I started using Laravel today, but I have some problems. Controllers don't run.
This is my controller:
<?php
class HomeController extends Controller {
/*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Show the application welcome screen to the user.
*
* #return Response
*/
public function index()
{
return view('welcome');
}
public function contact(){
return view(pages.contact);
}
?>
and this is my route:
<?php
Route::get('/', function () {
return "hello";
});
Route::get('contact','HomeController#contact');
?>
You need to add the namespace to the beginning of the controller:
<?php
namespace App\Http\Controllers;
You can also run this command when creating a controller
php artisan make:controller HomeController
In, addition as the other answer mentioned, the view name needs to be inside quotes.
Hope this helps.
This should be like this
public function contact(){
return view('pages.contact'); // View name must be inside ' '
}
also you dont need the closing tag for php ?>
These are my routes:
Route::get('login', array('as' => 'login', 'uses' => 'SessionController#create'));
Route::get('logout', array('as' => 'logout', 'uses' => 'SessionController#destroy'));
Route::resource('sessions', 'SessionController', array('only' => array('create', 'store', 'destroy')));
Route::get('/', array('as' => 'home', function()
{
return View::make('home');
}));
And this is my session controller:
<?php
use Latheesan\Repo\Session\SessionInterface;
use Latheesan\Service\Form\Login\LoginForm;
class SessionController extends \BaseController {
protected $session;
protected $loginForm;
public function __construct(SessionInterface $session, LoginForm $loginForm)
{
$this->session = $session;
$this->loginForm = $loginForm;
}
/**
* Show the form for creating a new resource.
* GET /sessions/create
*
* #return Response
*/
public function create()
{
return View::make('sessions.create');
}
/**
* Store a newly created resource in storage.
* POST /sessions
*
* #return Response
*/
public function store()
{
// snipped
}
/**
* Remove the specified resource from storage.
* DELETE /sessions
*
* #return Response
*/
public function destroy()
{
// Logout user
$this->session->destroy();
// Fire user logout event
Event::fire('user.logout');
// Success
Redirect::home();
}
}
This is the contents of SentrySession - http://pastebin.com/d3ijZHQv
Now, when I go to the logout route (for e.g. http://my-site.com/logout), it appears to be logging me out however I am not being re-directed back to home page. I just see a blank white page.
If I add something like dd('here'); after the Redirect::home(); line, I can see the word, which means the re-direction is broken/not working.
Any idea why this might be and how to fix it?
I have the error reporting (E_ALL & display_errors) turned on and laravel.log file is empty.
use this: return Redirect::home();