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)
Related
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
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.
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
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) {
//
});
});
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.