Laravel Controller Routes - php

I'm using a controlled route as such
Route::controller('company', 'CompanyController');
In this Controller i have a getLogin and postLogin function, the getLogin shows the login view. To get there i need to go to company/login
Now i'm wondering how can i redirect company/ to company/login
I have the following working but is it good practice?
Route::get('company', 'CompanyController#getLogin');
Route::controller('company', 'CompanyController');
Thank you!

In this case, index methods will respond to the root URI, so what you can do is create a getIndex() function which will return Redirect::to('company/login'). You can probably do a check on this for a logged in user first as well, for example...
public function getIndex()
{
if(!Auth::check())
return Redirect::to('company/login');
// Continue with what should happen if the user is logged in.
}
This way, when someone goes to /company, it will redirect them to login if they aren't logged in already or it will continue doing whatever you want it to do or redirect them to the same page you are redirecting people to after they login.
This also means you can do away with that Route::get() that you have setup for company.

Related

Laravel intended always returns email/verify

I'm using Laravel 7.4, and I want to redirect the users to the page they came from after the login. I edited the file RedirectIfAuthenticated and added the redirect like:
return redirect()->intended(RouteServiceProvider::HOME);
But the problem is, it always redirected me to home. When I actually logged what the intended function returns, it always returns the url as website.com/email/verify. I'm 100% sure I don't redirect the users to that url, because I am logging in with an already verified user.
It might be some FW setup that I failed to notice, since I took over this project from another developer. I'll provide any more info if needed.
Check for middleware maybe you are redirecting unverified users to email/verify
Then in your LoginController create a protected function redirectTo() and return url()->previous();
Your code will look like this
protected function redirectTo()
{
return url()->previous();
}
Or
Also you can add $this->redirectTo = url()->previous() to your constructor
like this
public function __construct()
{
$this->redirectTo = url()->previous();
$this->middleware('guest')->except('logout');
}
Hope this helps
If you dump dd(session()->all()) of a so-called "intended" page, you will see the following:
As you can see, Laravel store user's previously intended page at url['intended'] array element. So to imitate this behavior manually, you have to set this value manually where needed. In this case it's LoginController:
public function showLoginForm()
{
session()->put('url.intended', url()->previous());
return view('admin.auth.login');
}
Now the original location has been stored, and later when the user log in, they will be redirected as declared in RedirectIfAuthenticated#handle:
return redirect()->intended(RouteServiceProvider::HOME);
But what if the user went to the /login page intentionally, or simply put, what if they refresh the page? url.intended will have the value of route('login'), then when user logged in, they will be redirected to login page and the infinite loop continues.
In that case, url.intended should not have any value. RouteServiceProvider::HOME value will be used as destination. There must be additional condition for that, let's say your site have login route named login:
public function showLoginForm()
{
if (($url()->previous() != route('login')) {
session()->put('url.intended', url()->previous());
}
return view('admin.auth.login');
}
Case anything unclear, you may want to have a look at similar question

Laravel 8 Auth middleware protected route failing

I am building my first Laravel app with the Metronic 8 Laravel theme. It uses Breeze for authentication. I changed a couple of things around - created a welcome page for non-logged-in users, and moved the main template that was the index to an auth protected "/dashboard". The problem is that it still tries to load the dashboard Blade template, regardless of authentication, resulting in an error.
Route
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Here's Authenticate, where it should redirect non-authenticated users to the login page.
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
When I'm not logged in and navigate to the dashboard URL, it attempts to load the dashboard Blade template, which calls a menu function that checks the user permissions for menu items. Unfortunately, since there is no user, the application blows up from passing a null value to a method expecting a user array/object.
Any ideas on where to look for the problem? It seems to me that the auth middleware should redirect to the login page before trying to load the Blade template when not logged in.
I would put the middleware at the beginning of the route like this, though I'm sure it's not causing the problem-
Route::middleware(['auth'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Aside from that, please provide some information on the error itself like what the error is about/what is says..etc...
First of all, make sure you have a login named route defined in your routes/web.php file. It should look something like:
Route::get('/login', '<controller>#<method>')->name('login');
The important bit is ->name('login') so that the Authenticate middleware can correctly identify the route to redirect to. Change <controller>#<method> appropriately to route to the login method of your app.
Wakil's answer is irrelevant and actually opposite of the documentation. Your syntax is correct.
I figured out the issue. Keen Themes put a call to a method to build an array of menu items in the web routes file. That was making the call to the offending code. After I wrapped that in an auth check the error was fixed, and everything works as expected.

Create job only if you are login Laravel

When you click on create, it redirects to the next page.
If the user is logged in, redirects it to jobs/create, and if it is not to /login.
<h3>Create</h3>
What would an if statement look like in this case?
It would be something like
<h3>Create</h3>
You do not need an if statement. Laravel has built in middleware to detect if a user is authenticated, and redirects them to the login if they are not.
Either add the Auth middleware to your route like this:
Route::get('create', 'JobController#create')->middleware('auth');
Or add it to a constructor function at the top of your controller to exclude anyone who isn't logged in from accessing any of the functions:
public function __construct()
{
$this->middleware('auth');
}
https://laravel.com/docs/7.x/authentication#protecting-routes

Laravel Auth return to index

Can I redirect to the previous page someone if not logged in? Also, is it possible to create a hierarchy system for users?
I tried this:
if(!(Auth::check())) {
header("Location: {{ route('cooperado.index') }});
}
But i not even got an error message, just doesn't work. I'm starting at laravel so it's kind of hard to fully understand how it works.
Using Constructor in controller
You also can use middleware in order to redirect unauthenticated user back or somewhere else.
public method __construct(){
$this->middleware('auth');
}
Add this code in your controller so all methods within particular controller
-direct from route defination
Route::get('/path/',controller#method)->name('cooperado.index')->middleware('auth');
Redirection
using this method unauthenticated user will redirect to login page.
in order to edit redirection page you can change '#redirectTo' method
in
app/Http/Middleware/Authenticate.php
file.
if(!(Auth::check())) {
return redirect()->route('cooperado.index');
}
You mention in your question also wants to return previous page then use following but i'm not recommended this because if your first login effort fails then login failed' page becomes your previous page and second login effort succeeds then you are redirected to login page again because it's your previous page.
return Redirect::to(URL::previous());
You can use this. Is the easiest way to do this.
And you can also pass message, will displayed on when user redirect back from main page.
if(!Auth::check) {
return Redirect::back()->with('error','Please Login');
}

How to allow guest to access home page without login in Laravel 5.6

This has to be super simple, but I can't quite figure it out.
I want guest users (not logged in) to be allowed to go to the home page and not be redirected to the login page.
what is the trick?
Usually for authentification some middleware is used, so if you want the homepage to be accessible for unauthorized users you should remove auth middleware from homepage route.
You need to delete the middleware,
it could be in the routes file or in your Controller like this
routes:
Route::group(['middleware'=>['admin']], function(){
//Your routes
});
Controller:
public function __construct()
{
$this->middleware('auth');
}

Categories