I am using Laravel 4 for a web project , I am creating an admin panel
in the admin I have admin/profile/{id} to display user profile like firstname lastname etc..
in my AdminController I have :
// get the Admin Profile page
public function getProfile($id) {
// get the user from the database
$user = User::find($id);
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
but what happens I keep getting errors if I just go to admin/profile without any user id? , how do i make it work?
Basically how to make if the page doesn't exist to go to the dashboard or something like that ? for example if they tried admin/test and test is not a method there , if they are login it will go ot teh dashboard , if not it will go to login page?
You're asking two questions:
I get a 404 error if I go to admin/profile without a user ID. How can I redirect to the login page?
How can I redirect to a login page if the user is not logged in.
For the first question, you can do it a couple of ways. One solution is to add a route that matches anything after all your other route definitions:
Route::get('{any_url}', function(){ return Redirect::route("login"); });
This must be the last route defined because it will match any URL.
Another way to do it would be to catch the NotFoundHttpException in your start/global.php file. Add this code:
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
return Redirect::route("login");
});
Both of these examples redirect to a named route called login.
As for your second question, the correct way to handle this is to use the auth filter. In your filters.php file, you can add something like this:
Route::filter('auth', function($route)
{
// is the user authorized? if not, redirect to the login page
if (!user_is_authorized())
{
// redirect to the login page
return Redirect::route('login');
}
});
Where the user_is_authorized function is just shorthand for whatever check you do in your code. For information on using the auth filter, see http://laravel.com/docs/routing#route-filters.
You can simply add a missing handler (handles 404) like this:
App::missing(function($e){
// Log the missing url
Log::error($e);
// You may redirect to home
return Redirect::to('/');
// Or redirect to a 404 route that is declared
// to show a custom 404 page from that route
return Redirect::to('missing');
});
Put the code (given above) in your app/start/global.php file. For the missing url/route you need to crate add a route in your routes.php file like this:
Route::get('missing', function(){
// show the view: errors.missing
$this->layout->content = View::make('errors.missing');
});
Create a view as views/errors/missing.blade.php and in your missing.blade.php view, show a fancy message to inform the visitor that, the requested page/url is not available and add a link to your home page in that 404 page.
Read more on Laravel website about Errors & Logging, check Handling 404 Errors.
Related
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.
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');
}
This question already has answers here:
Redirect to a given Laravel URL
(6 answers)
Closed 8 months ago.
I'm using Laravel 5.2. I have a simple form on the homepage. When a user clicks search on that form it should send them to a registration page. It's just a dummy form so no data is being submitted. I'm not sure what I need to put in my routes file. Do I need to put something in my controllers file as well? Any help will be appreciated.
This is what I have on my main page...
form action="{{route('register')}}" method="post"
So you want to go to the "register" page when the form is submitted. Why would you want to do that? Though if you want to do that, #Vinicius answer is correct. I think what you want is - "If my users are logged in, then do the search, instead go to the register page".
To do what I am proposing, you will have to categorize your routes in middleware.
So in your routes file, you will define this route.
Route::post('search', ['middleware' => 'auth', function () {
return view('search');
}]);
This route will make sure that the "search functionality" method is only accessible when you are logged in. If laravel finds that the user is not logged in, it will automatically redirect the user to the register page.
For more information look at this code. This is the middleware functionality which decides if the user is logged in or not. As you can see, laravel by default will redirect the user to the login page. For you to redirect user to the register page, change the line to this.
return redirect()->guest('register');
You'll have to add a redirect statement with the form view, some like this, in your routes file:
Route::post('register' , function(){
return view('register.form');
});
You can add this return statement into a controller, and your route file will be something like this:
Route::post('register' , 'YourControllerName#register');
And your controller:
public function register(){
return view('register.form');
}
I'm developing a Slim application with twig engine for employees and would like to redirect the admin after adding a new user to the system to a page that will show the just added user. Now the problem is not getting the just added user but the redirection I'm doing it this way
//first render the page to add
$app->get('/api/employee/create', function () use ($app){
$app->render('add.html.twig');
$employee = R::findLast('employee', 'mobilenumber=?', array('mobilenumber'));
header('Content-Type', 'application/json');
})->name('add');
in the add.html.twig I have a form that collects the data and set the action to another route that does the recording into the database.
If I happen to add the following into the above app section
$app->redirect('/home');
then visit /api/employee/create the /home route gets shown first and it assumes the rest what would be the best way to show the add.html.twig template first then after the form submission return me to /home or any other route I set. Thanks
You can do something like this:
$app->get('/api/employee/create', function () use ($app){
$app->render('add.html.twig');
});
$app->post('/api/employee/create', function () use ($app){
$data1ToSave = $app->request()->post("data1_to_save");
$data2ToSave = $app->request()->post("data2_to_save");
// save datas
$app->redirect('/home');
});
same as title, i want to all people when using trang web of me, must be login (look like FB or Twitter, ...) with some of the required as follows:
If such as the current URL is the '/' (home page), systems display the interfaces registered. (display the rather than the redirect)
If such as a different URL '/' (home page), systems redirection to login page.
somebody can help me? I'm using laravel framework.
Laravel uses power thing called filter.
You can use them in any Route::action you want.
But a little exemple may helps you.
Following your request :
// Check manualy if user is logged. If so, redirect to the dashboard.
// If not, redirect to the login page
Route::get('/', function()
{
if (Auth::check()) // If user is logged
return View::make('dashboard')
return View::make('/login');
}
// Each routes inside this Route::group will check if the user is logged
// Here, /example will only be accessible if you are logged
Route::group(array('before'=>'auth', function()
{
// All your routes will be here
Route::get('/example', function()
{
return View::make('contents.example');
}
});
Of course, the filter auth is built in Laravel. You can find this file in app/filters.php
and modifying it to your needs.
As follow:
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::guest('/login');
});