Laravel - All route links are prefixed with sub-domain - php

I'm using a sub-domain in my application as a dashboard for a user account. In the views of the dashboard, I include a master template which contains links in the navigation that lead to the root of the website. When I visited the sub-domain I've noticed all links in the navigation were changed based on the sub-domain name.
In my master template, links are generated using the helper function route(). When a generated link is viewed on my sub-domain, the link changes from domain.com/about to sub.domain.com/about along with all other links.
Route::group(['domain' => 'dashboard.project.app', 'before' => 'auth'], function()
{
Route::controller('/', 'DashboardController');
});
Route::get('/', ['as' => 'home', 'uses' => 'HomeController#index']);
Route::resource('products', 'ProductsController');
Route::resource('support', 'SupportController');
So visiting dashboard.project.app would trigger getIndex() in the DashboardController while visiting project.app would trigger index() in the HomeController.
Great, we've got this far. Now I have a simple view I use as a template that will contain URLs to the named resource routes defined outside of the sub-domain.
Again, URLs are made using the helper function route(). In a template extended by a view called in DashboardController I would have a link in the navigation like:
Products
which would generate Products as desired however changes to Products when shown in a view under the sub-domain.
So I would like the desired output to always be project.app/products and never dashboard.project.app/products as visiting that link will result in a 404 as there is no getProducts() method in my DashboardController. It's the wrong links!
Get what I'm saying?

If the named route is not defined as being specific to a domain, then the url generator will use whatever domain you are currently on. If the route should be specific to a domain, specify it:
Route::group(['domain' => 'project.app'], function() {
Route::resource('products', 'ProductsController');
Route::resource('support', 'SupportController');
});
Now, even from your dashboard, route('products.index') should give you project.app/products.

Related

Laravel, same path on a different subdomain uses wrong controller

Question
How can I set up Laravel routing so that:
navigating to mysite.com/login uses the LoginController
navigating to somecompany.mysite.com/login uses the TenantLoginController
What I'm doing
I'd have a Laravel 5.7 app that has a typical login page at say, mystite.com/login
I'd like to set up a subdomain for this app like somecompany.mysite.com that will have it's own authentication.
I'd like the somecompany users to log in at somecompany.mysite.com/login
What I've tried
The route definition for the main site login
Route::group(['namespace' => 'App\Http\Controllers\Auth', 'middleware' => ['web']], function () {
Route::get('login', 'LoginController#showLoginForm')->name('login');
});
The rout definition for the subsomain login
Route::domain('somecompany.mysite.com')->group(function ($router) {
$router->group(['namespace' => 'App\Http\Controllers\Tenant\Auth', 'middleware' => ['web']], function($router) {
$router->get('login', 'TenantLoginController#showLoginForm')->name('somecompany.login');
});
});
What Happened
I can navigate to somecompany.mysite.com/login and the URL bar says somecompany.mysite.com/login but when I do, the request is actually routed to the 'LoginController#showLoginForm' controller not the expected 'TenantLoginController#showLoginForm' and the typical login form is desplayed, not the subdomain's login form.
If I change the path to $router->get('tenant-login' and navigate to somecompany.mysite.com/tenant-login the subdomain login form is shown, and somecompany.mysite.com/login shows the main login form.
Since you did not specify a domain in the first route (handled by LoginController), it should also be valid for the somecompany.mysite.com subdomain.
To work around that, I would suggest trying to add more specificity to that first route, enclosing it with Route::domain('mysite.com').
The Laravel router always takes the first matching route, and that first one matches just fine in the end.

Laravel 5.5 Routing and Sub-domain Routing

I am very new in Laravel. I currently created my personal site on Laravel 5.5 and uploaded to GoDaddy server: http://bhattraideb.com/. Now when I click on 'Blog' from navigation I would like to redirect like http://blog.bhattraideb.com/ which is currently redirection to 'bhattraideb.com/public/blog'.
Again from the same this link 'bhattraideb.com/public/blog' when I click on 'Resume' I aspect to redirect on 'bhattraideb.com/'
Here is my route code
//** Resume Routes
Route::prefix('/')->group(function() {
Route::get('/', 'Frontend\ResumeController#index')->name('resume.index');
Route::resource('resume', 'Frontend\ResumeController');
});
//** Blog Routes
Route::get('show/{id}', 'Frontend\PostController#show')->name('post.show');
Route::get('blog/{slug}', ['as' => 'post.single', 'uses' => 'Frontend\PostController#getSingle'])->where('slug', '[\w\d-\_]+');
Route::resource('blog', 'Frontend\PostController');
Can anyone please guide me for this.
Thanks in advance.
You can read up on https://laravel.com/docs/5.5/routing#route-group-sub-domain-routing for more details on this but the following simple example should work:
web.php
Route::group(["domain" => "blog.bhattraideb.com" ], function () {
Route::get("/", "Frontend\PostController")->name("blog.index");
//All blog routes should be defined in here
});
You can then use the helper route("blog.index") to get the URL along with the subdomain when generating links to the blog.
Note that you will need to setup the webserver to accept blog.bhattraideb.com as an alias of bhattraideb.com. Laravel will then sort the rest out

Sub-Domain Routing Laravel 5

I have been testing sub-domain routing functionality in Laravel 5 and have had success with the following code as described in the docs. When the user visits {username}.mysite.com, the user profile view is shown as expected.
Route::group(['domain' => '{username}.{tld}'], function () {
Route::get('user/{id}', function ($username, $id) {
//
});
});
But, I was expecting a bit of different behavior from what I am experiencing. When the user visits the site through the sub-domain, all of the links in my views now retain the sub-domain within them. All of the other links like {username}.mysite.com/home and {username}.mysite.com/login etc... are fully functional, but I don't understand why Laravel is populating all of my links with the sub-domain and/or how I can get rid of this and only retain the sub-domain for just the single route. I want all of the other links in my views to be like mysite.com/home and mysite.com/login. I was hoping to just use {username}.mysite.com as a quick access point for site visitors, not to retain it in all views.
What can I do to change this behavior?
Move routes you don’t want prefixing with the subdomain outside of the route group:
// These routes won’t have the subdomain
$router->controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
// These routes WILL have the subdomain
$router->group(['domain' => '{username}.{tld}'], function ($router) {
$router->get('/', 'UserDashboard#index');
$router->controller('account', 'AccountController');
$router->resource('users', 'UserController');
});
You forgot to redirect user... So:
First, as Martin Bean suggested, exclude undesired controllers from sub-domained group.
Second, after user's successful login - redirect him to address without subdomain. You can do that by overriding auth middleware with yours implementation (which must implement TerminableMiddleware interface).
I. e.:
User was on page https://auth.example.com and logined.
Your's override of auth middleware checks for succesful login.
... and redirects user to https://example.com/home or whatever...
That should be enough.
I found the problem where all of my links/routes were being prefixed with the subdomain, even when they were outside of the route group. The issue is with the Illuminate HTML link builder. It renders relative links rather than full absolute links.
So instead of using: {!! HTML::link('contact', 'Contact Us') !!}
I should have used: {!! HTML::linkRoute('contact_route_name', 'Contact Us') !!}
The linkRoute() function takes into consideration the route group and applies the subdomain as required.

How to redirect to a base route in Laravel

I have an issue in laravel routing ,
I am able to go to the following routes independently:
*localhost/jmeercat/public/home*
*localhost/jmeercat/public/manageProject/newlycreatedproject*
But if I am on the route *localhost/jmeercat/public/manageProject/newlycreatedproject*
and click on home page link I am redirected to localhost/jmeercat/public/manageProject/home path instead of localhost/jmeercat/public/home.
I am using return Redirect::to('home') method.
Check the Laravel documentation on Named Routes - http://laravel.com/docs/routing#named-routes
Named routes make referring to routes when generating redirects or URLs more convenient. You may specify a name for a route like so:
Route::get('/home', array('as' => 'home', 'uses' => 'HomeController#home'));
You may use the route's name when generating URLs or redirects:
$url = URL::route('home');
$redirect = Redirect::route('home');

Laravel: Nesting controllers and making parent controller redirect to children?

This is my first project in Laravel, so play nice with me!
The goal is to create a CMS. Every page will have it's own "slug", so if i name a page This is a test, it's slug will be this-is-a-test. I want to be able to view that page by going to example.com/this-is-a-test.
To do that, i'm guessing i'll have to do something like:
Route::any('(:any)', 'view#index');
And create a controller called View with an index method. All good, right?
The problem is creating the admin area. I'm gonna have a few pages within the area, a few examples are Dashboard, Pages, Settings and Tools. Since all of those are sub-pages in the admin, i figured it would be appropriate to make them nested controllers, right? The only problem is, when i visit /admin, i want to show the dashboard (/admin/dashboard) directly. I would prefer just calling the dashboard controller instead of redirecting to /admin/dashboard from the admin controller. Is that possible?
So, to illustrate what i mean:
example.com/admin -> loads admin.dashboard
example.com/admin/dashboard -> also loads admin.dashboard
Here are all my routes:
Route::get('admin', array('as' => 'admin', 'use' => 'admin.dashboard#index'));
Route::get('admin/dashboard', array('as' => 'admin_dashboard', 'use' =>
Route::any('/', 'view#index'); // Also, should this be below or above the admin routes? This route will show the actual cms pages.'admin.dashboard#index'));
And here is my admin_dashboard controller:
class Admin_Dashboard_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return 'in dashboard';
}
}
The view controller just displays a link to the admin page, that works. I just can't figure out what's wrong with the admin routes? When i go to /admin or admin/dashboard i just get a blank page, no 404. If i go to admin/blah or just blabla i get a 404, so i know something is happening, it's just not happening correctly. Am i missing something?
I had misunderstood the naming conventions for controllers.
This is how it was:
controllers
admin
admin_dashboard.php Containing controller Admin_Dashboard_Controller
It's suppose to be:
controllers
admin
dashboard.php Containing controller Admin_Dashboard_Controller
In other words, i shouldn't have prepended "admin" to the beginning of the controllers file name. It all works fine now.
I was actually able to minify the routing code to this as well:
Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' => 'admin.dashboard#index'));

Categories