Sub domains using laravel - php

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

Related

Laravel routing based on domains

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.

How i can make referal url prefix with dynamic value to accessible in all website

For guest user i want to create specific referal link as dynamic prefix with which website can be accessed for eg 127.0.01:8000 same on siteurl/user/{dynamic value}/ or other front end pages with these prefix.
I have implemented package (mcamara's) but its not working in my case.
In your routes file you can do something like this:
Route::group(['prefix' => '/user/{code?}'], function () {
Route::get('/', 'ReferralCode#code');
});
Then in your controller you can access it like so:
$code = $request->input('code');
Then you can do whatever you want, like validate the code exists, etc.
You can learn more at the docs here: https://laravel.com/docs/5.8/controllers#basic-controllers

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-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.

Prevent access to /index (default Controller action)

My routes.php looks like this:
Route::controller('/', 'HomeController');
The default root action is getIndex(). Typing the address [DOMAIN NAME]/index though, returns the start page as well. How do I prevent this? I want the start page to only be accessible when going to the root URI of my project (/).
You are using RESTful Resource Controllers, replace it with,
Route::get('/', 'HomeController#getIndex');
OTHER OPTIONS
/ refers to index method in the controller by default, you could make index refer to another method which creates the view you want like.
Route::get('index', 'SomeController#someMethod');
in your route.php file
If you want the page to show a 404 you can use in your SomeController#someMethod
public function someMethod()
{
App::abort(404)
}
Or without the use of a method
Route::get('index', function() {
App::abort(404)
})
Like pointed out in the comments, you need to blacklist the /index route. But only that one! You don't need to blacklist every route you don't want your users to see, because Laravel will handle that for every route that is not defined. But index is a special case, because you have to name the default route some name, don't you?
So long talk, short meaning: Rest assured, you will only have to blacklist each index route, if it is that important to you.
I solved it like this:
Route::get('/', 'HomeController#index');
Route::controller('/', 'HomeController');
There's a reason I want a controller that takes care of all my routes :)

Categories