Laravel routing based on domains - php

I am redeveloping a core-PHP web app to Laravel 5.8 application with (example) main domain: www.maindomain.com, and few route groups based on url prefix:
www.maindomain.com/user
www.maindomain.com/admin
www.maindomain.com/teacher
The app also has around 50 addon-domains, in the old app, all of them were pointing to one location (example: public_html/addondomains), and the content that was displayed was determined by the $_SERVER['http_host'].
I want to do the same in Laravel, but i can't set up the route rule for the addon domains. So, it needs to route any url that is not maindomain.com to a controller function. Any help? :)

You can use Route::domain()
Example:
//http://www.maindomain.com/test
Route::domain('www.maindomain.com')->group(function () {
Route::get('/test', 'HomeController#test')->name('main.test');
});
//http://example.maindomain.com/test
Route::domain('{addon}.maindomain.com')->group(function () {
Route::get('/test', 'AddonController#test')->name('addon.test');
});
https://laravel.com/docs/5.8/routing#route-group-sub-domain-routing
Make sure you server is configured to serve these domains from single Laravel installation.

Related

Laravel routing: Route in domain group visible also in local environment

What I need:
My app has a public domain
All routes in my Admin controller should be opened only if the remote domain is domain1.com and also in local environment.
Currently: if I put the admin panel route in the group, it is not visible in local environment any more, making it difficult to develop.
// My secret domain, accessible only for admins
Route::group(['domain'=>'domain1.com'],function(){
Route::get('admin-panel', [App\Http\Controllers\Control\AdminController::class, 'admin_panel']);
});
// To be accessible both in domain1.com and domain2.com:
Route::get('homepage', [App\Http\Controllers\Control\PagesController::class, 'homepage']);
Solutions
My current solution:
in route file web.php I add extra line
if( \App::environment() == 'local') {
Route::get('admin-panel', [App\Http\Controllers\Control\AdminController::class, 'admin_panel']);
}
but it is a crude, temporary fix.
TODO:
Either in route file or in the controller. A filter in a controller (for all or selected methods) would be best.
An if clause checking if either the environment is local or the domain is domain1.com
Thank you.
I think laravel not support something like that. But you can declare the routes to a function variable and then use it in each domain.
$adminRoutes = function() {
Route::get('admin-panel', [App\Http\Controllers\Control\AdminController::class, 'admin_panel']);
};
Route::group(array('domain' => 'domain1.com'), $adminRoutes);
Route::group(array('domain' => 'localhost'), $adminRoutes)

Laravel route exist but unable to view page

I have been invited to collaborate on a laravel 7 project and I have been able to set up the project locally on a windows 10 system using wamp server. While reviewing the project, I noticed the plan is to use subdomain routing. I am currently tasked with setting up the blade templates and I want to test the route but I can't get the route to work correctly even though the routes exist. This is what the routes look like
When i try viewing the page for realestate by calling the url like this realestate.localhost:8000 I get the connection error below
The route is inside routes/realestate.php folder and its using a closure
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return 'Realestate Routes';
});
What is the right way to call the route in my local enviroment on windows?
I'm not sure if your environment is different from mine, but make sure you're accessing via http:// and not https://
I just saw your Route code - it's been a long day, sorry :P
Try reading up on Subdomain routing here.
Your Route should look like this:
Route::domain('realestate.localhost')->group(function () {
Route::get('/', function () {
return 'Hello, world!'
});
// all other routes for realestate.localhost go in here
});
You need to edit your hosts and add redirect to localhost from your subdomain. see https://stackoverflow.com/a/56921347/11775360

Laravel 5 one instance, multiple identical subdomains

I would like to use one server that uses a base Laravel installation, and have subdomains that reference that installation. All the subdomains will be the same like an SaaS.
I've looked around and the databases connections are easy, but I'm wondering if you can do this intelligently with the same codebase for subdomains.
The subdomains world include the minimal needed files for its subdomain -- perhaps, the public index and bootstrap? Hopefully without symlinking everything.
I'm not worried about the server configuration, I just would like to be pointed in the right direction for Laravel code, like middleware to handle the request then point to that subdomain?
A lot of threads I've read don't have an answer that seems standard, any ideas or links?
Also, if it were a multi server setup wouldn't one be OK with an NFS server for the core?
With laravel you can check the URL without using subdomains but just group routing requests.
Route groups may also be used to handle sub-domain routing.
Sub-domains may be assigned route parameters just like route URIs,
allowing you to capture a portion of the sub-domain for usage in your
route or controller. The sub-domain may be specified using
the domain key on the group attribute array:
Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) {
// your code
});
});
Read more about this on laravel documentation https://laravel.com/docs/5.4/routing#route-group-sub-domain-routing
BOUNTY
You can also supply more parameters to the same Route::group, that could be, for example
Route::group(['domain' => '{subdomain}.{domain}.{tld}'], function () {
Route::get('user/{id}', function ($account, $id) {
// your code
});
});
In the same time, you can decide to limit the domain parameters you are going to accept using the Route::pattern definition.
Route::pattern('subdomain', '(dev|www)');
Route::pattern('domain', '(example)');
Route::pattern('tld', '(com|net|org)');
Route::group(['domain' => '{subdomain}.{domain}'], function () {
Route::get('user/{id}', function ($account, $id) {
// your code
});
});
In this previous example, all the following domains will be accepted and correctly routed
www.example.com
www.example.org
www.example.net
dev.example.com
dev.example.org
dev.example.net

Sub domains using laravel

I have one domain, and want to put all of my scripts there. So that they can be accessed like this: script.mydomain.com, anotherscript.mydomain.com and so on.
Pretty much like google, they have their main www.google.com domain, and for drive they have: drive.google.com.
I didn't find anything related to sub domains in the laravel docs.
Can anyone please explain how to achieve this?
Use route groups with domain key.
Route groups may also be used to handle sub-domain routing.
Sub-domains may be assigned route parameters just like route URIs,
allowing you to capture a portion of the sub-domain for usage in your
route or controller. The sub-domain may be specified using the domain
key on the group attribute array:
Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) {
//
});
});

URL routes for Lumen (Laravel) and script paths on Homestead

I am building an app with Lumen for the backend and angular for the frontend. Lumen handles routes and serves the basic templates with a header and footer, where angular takes over to control the content. I am trying to add a url parameter to a route, but it breaks all my paths to scripts as it sees it as a subdirectory not a parameter. My route looks like this in Lumen:
$app->group(['prefix' => 'user', 'middleware' => 'auth'], function($app) {
$app->get('{any}', function() {
return view('index');
});
$app->get('detail/{userId}', function() {
return view('index');
});
});
I have a url of example.com/user/create that works fine, but as soon as I use example.com/user/detail/101 it breaks. How do I set it up so all my angular paths are not destroyed as I add parameters? I would like to stay away from adding the absolute url path as I really don't want to manage differing urls through dev/stage/production environments.
EDIT:
The following routes work and do not break css/script paths:
example.com/user
example.com/user/create
The following route does break paths:
example.com/user/detail/101
Add the any route after the detail/{userId} route.
In general, any "catch-all" routes need to go at the end so they don't interfere with anything.

Categories