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]);
}
Related
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
In Laravel 5.4 I want to prefix locale with base url.When i run php artisan serve then i get
notfoundhttpexception
. It does work if i put manually http://localhost:8000/en. what i want now, when i will run php artisan serve it should redirect to http://localhost:8000/en.
Following is the route file:
Route::group( ['prefix' => App::getLocale() ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
});
If I understand your problem properly, You need use code like this:
Route::group( ['prefix' => '{locale}' ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
//Setup locale based on request...
App::setLocale(Request::segment(1));
});
But the better way I can suggest is use locale setup like:
if (in_array(Request::segment(1), ['en', 'fr'])) {
App::setLocale(Request::segment(1));
} else {
// set your default local if request having other then en OR fr
App::setLocale('en');
}
And just call route like:
Route::group( ['prefix' => '{locale}' ], function()
{
Route::get('/', array('as' => 'home.index', 'uses' => 'HomeController#getIndex'));
});
By this code you can setup locale based on route prefix dynamically.
I'm in the process of upgrading our web app from laravel 4.2 to laravel 5.2. I've managed to solve most of the problems but this particular problem is leading me in loops.
This is how the route group for admin dashboard looks like:
Route::group(['middleware' => 'web','prefix' => 'adm'], function ()
{
Route::get('login', ['as' => 'admin.login.view', 'uses' => 'AdminLoginController#loginView']);
Route::post('login', ['as' => 'admin.login.attempt', 'uses' => 'AdminLoginController#attempt']);
Route::get('logout', ['as' => 'admin.logout', 'uses' => 'AdminLoginController#logout']);
...other routes pertaining to admin dashboard
}
The login functions and all functions within the admin panel work as expected. The only problem is when the user logs out, any one can access the remaining routes in the admin panel (no login required). I have placed Auth::check() and checked for auth in various controllers, the login and logout work as expected.
Auth::check() fails if user is not logged in and passes if user has logged in.
How do I make sure all the routes within this group are accessible only to logged in users. I have tried creating another middleware called authAdmin and tried to use that instead of the web middleware. In that case I can't even login.
I create new middleware for login and in the page look like this
namespace App\Http\Middleware;
use Closure;
class Login
{
public function handle($request, Closure $next)
{
$messages = config('message');
if ($request->session()->has('userId')) {
return $next($request);
}
return redirect('/')->withErrors("Please login first");
}
}
In Kenel.php register Login class
protected $routeMiddleware = [
'login' => \App\Http\Middleware\Login::class,
...
In route file
Route::group(['middleware' => ['web'],'prefix' => 'adm'], function () {
Route::get('login', ['as' => 'admin.login.view', 'uses' => 'AdminLoginController#loginView']);
Route::post('login', ['as' => 'admin.login.attempt', 'uses' => 'AdminLoginController#attempt']);
Route::get('logout', ['as' => 'admin.logout', 'uses' => 'AdminLoginController#logout']);
Route::group(['middleware' => 'login'], function () {
[Your other route here]
});
)};
Hope this help
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
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')