Do in middleware http://laravel.com/docs/5.1/middleware
Redirect to url from blade
In my application I am checking a condition in the blase and if that condition is not true redirect to url. What I want to do is something like this
@if(isset($user))
//Display the page
@else
//Redirect to login page
@endif
how to redirect to login page in the else block if user is not logged in
The route for login in routes.php is
Route:get('login',LoginController@loginpage);
Please Help!
@veve286
For some reason I cant use middleware. I have to check it in master page. Also how do I set the $user variable which is accessible for every page.
I don't see a reason why you wouldn't be able to use middleware. Please explain yourself
You should be able to use middleware, but aside from that, your first post also has a problem.
Route:get('login',LoginController@loginpage);
You need to surround LoginController@loginpage with quotes.
@bobbybouwmann I have written api's which are called from a device. Hence to avoid token mathching I disabled the middleware. Now I realized that I can just comment the verify token line in kernel.php rest middleware can be used. I will use the middleware now. But just to know, how do I redirect from the master page and check if user is set or not.
For your needed, here is one way to solve using javascript.
@elseif
<script type="text/javascript">
window.location = "{ url('/login') }";//here double curly bracket
</script>
@endif
here share all data. http://laravel.com/docs/master/views#sharing-data-with-all-views
@maheshk If you really need to redirect from the view, you can use javascript. For example, this should redirect you to google.
@if(isset($user))
//Display the page
@else
<script>window.location.href = "http://google.com";</script>
@endif
I am using the same controller for calls from the device as well as from the views. So if I have the middleware enabled the will the calls from the device be served because the device is checking for the user login on the device itself and the user credentials are on the device and not on the server.
So what is the problem right now? You can't use the middleware? Or you won't use the middleware?
how do I redirect from the master page and check if user is set or not.
It is a nonsense to redirect something "from a view". It is the responsibility of the controller to return the right response. Redirect in the middleware or in the controller.
@bobbybouwmann I have api's that are called from device as well as the Web UI. They are written in the same controller. Say I have operation related to a Developer I have written it in the DeveloperController irrespective of the part from where they are called. Is there a way that I can serve both the device as well as the Web UI using the same controller and using middleware for that controller.
Is there a way that I can serve both the device as well as the Web UI using the same controller and using middleware for that controller.
If you send a request to a url, who cares from which device it comes from ? It goes through the middleware and run the action thats all. Why do you think it would be different for each device ?
The middleware will run on your (web)server and not in your client (device, web ui). So no matter what url or api you call as long as your route uses the middleware you will be protected against it.
@pmall I know it wont be different, my point is I dont have to check authentication if the request is from device, but if it is from web UI then I need to check the authentication. Hence, I am not using middleware, rather checking in the master.blade.php if the the user is logged in or not. Am I going on the right path?
Am I going on the right path?
No.
Why the user from devices doesnt have to authenticate ?
You can create two urls, one for devices (something like http://api.domain.com/foo), one for the UI http://domain.com/foo which points to the same controller action, and you put the middleware for auth only for the url for UI.
@pmall The users from the device are authenticated on the device itself. So, if I am not wrong, what you are saying is I keep my api's at two different places and the one serving the web UI can have the middleware enabled. Is that what you are saying? From different places I mean they both have separate url's.
The users from the device are authenticated on the device itself.
How do it send data to the server to retrieve their sessions ?
The device actually works in offline mode. When it is online it send the JSON of all the collected data. I read the json file and store the data to database. While retrieving the data the device sends the get request and the api sends the requested data.
When it is online it send the JSON of all the collected data.
So how do you retrieve the user session ?
@pmall That is something the device takes care of. I think they are storing the credential locally on the device.
ok I don't understand anything.
Show us the data send by a device ?
Let me try once again.
I receive data from the device which I have to store as is to the database. I dont have to care where it came from, how it came.
From web UI if a page is requested I have to check if the user is logged in or not. If not redirect the user to login page.
I dont have to care where it came from, how it came.
So why you have to use the same controller action ? use an action to store data from devices, and another for displaying the view to the web UI user.
By action do you mean the method/function of the controller?
Yes it is called an action
I am using different actions in the same for storing and displaying the data. Correct me if I am wrong, middleware is applied to the whole controller. Can middleware be applied to a specific action?
middleware is applied to the whole controller.
No. You should associate middleware to actions in the route.php file. And you should have to different actions for storing data and displaying it.
http://laravel.com/docs/master/middleware#registering-middleware
@pmall I am using route resources how can I then associate the middleware to action
You can make middleware like this. It will only check the request is not ajax.
public function handle($request, Closure $next)
{
//if request is not ajax check auth otherwise go normally
if( ! $request->ajax( ) ){
if ($this->auth->guest()) {
return redirect()->guest('/login');
}
}
return $next($request);
}
I am using route resources how can I then associate the middleware to action
By specifying your routes manually. resource is for prototyping.
Please or to participate in this conversation.