Laravel 'before' route not working - php

I have these three Routes
Route::get('login', [
'uses' => 'loginController#showLogin',
'as' => 'login.show',
'before' => 'guest'
]);
Route::get('dashboard', [
'uses' => 'DashboardController#index',
'as' => 'dashboard.show'
]);
Route::filter('guest', function()
{
if (Auth::check())
return Redirect::route('dashboard.show');
});
When I log-in the Auth:check() recognize it, but instead of redirecting me to localhost:8000/dashboard it redirects me only to localhost:8000
Am'I doing something wrong?
Thank you very much for any suggestions.

Redirect::route() will only redirect to saved routes. You have named your first route 'login.show'. Your route 'dashboard' is actually named 'dashboard.show'. You will need to do Redirect::route('dashboard.show')
If you are just wanting to redirect to a URL then do Redirect::to('dashboard')

Related

Laravel named routes grouping all admin routes

i just want to group all my admin routes in my laravel. I'm a beginner in laravel and i want to synchronize all my admin routes in one group, my question is, why i cant put the post route inside the group of my admin routes?
Here is my routes:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
});
my above code was returning error , where laravel says admin.login route doesn't exist. Then i tried to put the post route outside the group and it works. Why?.
Here is the code where returns no error:
Route::group(['as' => 'admin::', 'prefix' => 'admin'], function () {
Route::get('login', [
'as' => 'login',
'uses' => 'admin\AdminLoginController#index'
]);
});
Route::post('login', 'admin\AdminLoginController#auth')->name('admin.login');
Because you use as in your route group and it's admin:: and you may link to admin.
Now it goes to admin::login and you need admin.login

Laravel advanced routing system

I'm trying to implement a simple CMS with Laravel 5.2 which basically handles two kinds of routes. The first one is used to browse a website view, that has to be {view}.html. The controller iterates the database records and if it can't find that page, will return a 404 error page:
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
For example these routes will match:
www.mydomain.ext/homepage.html
www.mydomain.ext/about.html
www.mydomain.ext/news.html
www.mydomain.ext/contact.html
and so on. The second one is a route group for the admin control panel:
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
So all the routes in this group will be something like:
www.mydomain.ext/admin/dashboard
www.mydomain.ext/admin/user/1
www.mydomain.ext/admin/page/2
and so on.
From what I've found here:
Laravel matches routes from the top down. So all you need to do is put 'campaigns/add' above the wildcard route.
And that's what I've done:
routes.php
Route::group([
'prefix' => Localization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect']
// LaravelLocalization (https://github.com/mcamara/laravel-localization)
], function() {
Route::auth();
// admin routes
Route::group([
'prefix' => env('ADMIN_PREFIX', 'admin'),
'as' => env('ADMIN_PREFIX', 'admin') . '::',
'middleware' => ['auth']
], function() {
/*
* ADMIN ROUTES
*/
});
Route::get('/{page}', [
'as' => 'page',
'uses' => 'Website\WebsiteController#showPage'
])->where(['page' => '.+(\.html)']);
});
But when I try to call an admin route, Laravel throws this error:
Missing argument 1 for App\Http\Controllers\Website\Core\WebsiteCoreController::showPage()
So I suppose I'm doing something wrong... Any suggestions on how to fix my code?
Thanks everyone in advance

Laravel 5 Routing admin site error

I can't go to the admin part of my site. I'm landing always on the main site. This problem is only with admin, every another routes working perfectly.
Routes:
Route::group(['middleware' => ['web']], function () {
// Login, logout
Route::get('admin', array('as' => 'login', 'uses' => 'AuthController#index'));
Route::post('admin/login', array('as' => 'cms_login', 'uses' => 'AuthController#login', 'before' => 'csrf'));
Route::get('admin/logout', array('as' => 'logout', 'uses' => 'AuthController#logout'));
}
AuthController:
public function index(){
// Show login form
return view('backend/pages/login');
}
Doesn't matter what I do, I landing always on the home screen www.example.com if I try to call www.example.com/admin.
The problem was the app/http/Middleware/RedirectIfAuthenticated.php
Here was "return redirect('/');" that's why I landed always on the home screen

Laravel 5.1 named route resolution with sub-domain not working

Thanks for any help you can provide. I am 10 days into learning Laravel 5.1 so any suggestions on what I have missed will be greatly appreciated!
I am having an issue with the resolution of named routes in Laravel 5.1. I am building an app that will have a URL in the format of {organisation}.website.com, where {organisation} is defined at registration of customer.
The code routes perfectly when using sample subdomains, so long as I hardcode the route address (e.g.: redirect('/home');), but when I try and route by named routes from Controller (e.g.: redirect()->route('session.create');) the routes resolve like this:
http://%7Borganisation%7D.website.com/home
My routes look like this:
<?php
/**
* Entity routes - resolves {organisation}.website.com
*/
Route::group([
'domain' => '{organisation}.' . env('APP_DOMAIN')
], function(){
/*
|----------------------------------------------------------------------
| Freely available routes for login, registration, password reset etc
|----------------------------------------------------------------------
*/
Route::group([
'middleware' => 'guest'
], function(){
// Login
Route::get('login', ['uses' => 'SessionController#create', 'as' => 'session.create']);
Route::post('login', ['uses' => 'SessionController#store', 'as' => 'session.check']);
});
Route::group([
'namespace' => 'Website',
'middleware' => ['authorise'],
], function(){
});
/*
|-----------------------------------------------------------------------
| Potentially secured routes
|-----------------------------------------------------------------------
*/
Route::group([
'middleware' => ['authorise']
], function(){
// Logout and destroy all Auth data
Route::get('logout', ['uses' => 'SessionController#destroy', 'as' => 'session.destroy']);
});
});
In my Controllers I call the routing like this:
return redirect()
->route('session.create')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
and it successfully completes, but with the above URL and 404. If I change to this it works perfectly.
return redirect('/home')
->withInput($request->only('email', 'remember'))
->withErrors([
'email' => $this->getFailedLoginMessage(),
]);
What have I missed in my organisation sub domain set up to make named routes work? Thanks!
Got it working. Below is how I fixed the issue, which essentially I had to bind on the fly to the route the slug of the sub-domain. Hope this helps someone!
I first had to pass the Organisation class to my controller methods and change the route calls like this:
public function destroy(Organisation $organisation)
{
Auth::logout();
return redirect()->route('session.create', ['organisation' => $organisation->slug]);
}

How to redirect an unauthorized user to the login page in Laravel?

I'm new to Laravel ( version 3 ), i do not know how to set Route and filters in Laravel so that any unauthorized user that is trying to access any url redirects to the login page (NOT the 404 error), in another word the default home page for unauthorized users is going to be the login page and for the authorized users it's going to be the dashboard.
If you are using laravel Auth class you can create an authorized route group. All routes that are defined there will be redirected if the user isn't logged in. Your router file will look something like this:
Route::get('/', array('as' => 'intro', 'uses' => 'intro#index'));
Route::get( 'login', array('as' => 'login', 'uses' => 'user#login'));
Route::get( 'logout', array('as' => 'logout', 'uses' => 'user#logout'));
// PROTECTED
Route::group(array('before' => 'auth'), function()
{
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'user#dashboard'));
});
// AUTH FILTER
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('login');
});
Just put a before filter in the declaration of the route like this
Route::get('edit_profile', array('before' => 'auth', function()
{
return View::make('profile.edit');
}));
The Auth filter exists by default in Laravel.

Categories