Laravel Wildcard Subdomain for User - php

I am looking to implement a whitelabel solution to my platform and need to implement wildcard subdomains for or system, the only issue is our system sits on a subdomain its self. So I need to filter out anything that comes to a specific subdomain.
// *.website.co.uk
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', function ($element) {
dd($element);
});
});
// my.website.co.uk
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
However using the code above I get the error:
Undefined variable: element
How would I avoid this error?

A good way is to exclude 'my' using a pattern. Put this code at the top of your routes file:
Route::pattern('element', '(?!^my$)');
Alternatively, that can go in the boot() section of your RouteSericeProvider. To give you a working solution, your code becomes the following (you can tidy up later!)
Route::pattern('element', '(?!^my$)');
Route::group(['domain' => '{element}.website.co.uk'], function() {
Route::get('/', 'PagesController#getLogin');
Route::post('/', 'PagesController#postLogin');
});
An alternative way is to match the 'my' route before matching the {element} route. However, while many do this I think it might be harder to maintain if the ordering of routes isn't clearly explained in the comments.

Related

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

Laravel Middleware / Route Groups

I'm fairly new to Laravel, so this question may obvious to some.
In the case of running checks per HTTP request, for example User Authentication. Is there a better, more efficient or simple correct way to run these checks. From my initial research it would seem that this could be accomplished using either MiddleWare, eg.
public function __construct()
{
$this->middleware('auth');
}
It also seems like it would be possible using routing groups, eg.
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
Is there any benefits of doing this either of these two ways? Apart from the obvious benefit of not having to put $this->middleware('auth'); in every controller auth would need to be checked.
Thanks
Edit..
After taking on your advice I attempted to utilities the route grouping to control my Auth MiddleWare. But this has seemed to have broken my site.
Route::group(['middleware' => 'auth'], function () {
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get ( '/redirect/{provider}', 'SocialAuthController#redirect' );
Route::get ( '/callback/{provider}', 'SocialAuthController#callback' );
});
Am I missing something obvious?
You are almost there, just remove the Route::auth():
Route::group(['middleware' => 'auth'], function () {
Route::get('/home', 'HomeController#index');
//add more Routes here
});
The suggested options did not work for me but when I checked the laravel documentation, I found this:
Route::middleware(['web'])->group(function () {
//Your routes here
});
It works for me. Laravel 8.*
There is no real difference, personally i use groups for the standard middleware and put exceptions in the construct
Using Route group is easy for maintenance/modification , other wise you will have to remember each controller where you are using certain middle ware, of course this not a concern in a small medium sized application, but this will be hard in a large application where is lots of controller and references to middle ware.

In Laravel, How can I apply a filter on all routes of a website except one specific route?

In Laravel PHP Framework, How can I apply a filter on all routes/pages of the website except one specific route/page?
Update:
It will be great if there is a way other than (route groups)?
use route groups
// unsecured routes.
Route::get('/', 'UserController#getLogin');
Route::group(array('before' => 'yourFilter'), function()
{
// secured by filter `yourFilter`.
Route::controller('route1', 'XxxController');
Route::post('user/save', function() {
// content
});
Route::get('user', 'UserController#getUser');
});

Laravel - Change base URL globally?

Let assume, the base URL of my application is - http://www.example.com (I have not set anything in any config file to specify this). There are a lot of hard coded urls in the application.
eg. Contact
Now, if I go though the application using an URL like - http://www.example.com/country, is it possible to assign a global base URL, where when I click on contact it will take me to - http://www.example.com/country/contact.
There are a lot of such hard-coded URL, changing it individually will take a lot of time (like appending it with a global variable). Is there any simpler way to do this or is there any config specific for this in laravel? I am fairly new to laravel. Any help would be appreciated.
You can use the somewhat cumbersome solution of applying a filter, as suggested by #worldask, but I think it would be better to set a named route and change all occurrences using a regular expression (any decent editor allows for that). That way, for the lifetime of your application you only need to change the routes in routes.php, and it will be reflected everywhere.
e.g
Route::get('country/contact', ['as' => 'contact',
'uses' => 'SomeController#someFn'];
Contact
Of course, the same principle applies to adding a prefixed group of routes, so you can wrap the entire routes file with a group prefixed by 'country'.
you can try Route Prefixing
Route::group(array('prefix' => 'country'), function(){
Route::get('Contact', 'HomeController#index');
Route::get('another', 'HomeController#index');
});
edit
try route filter
Route::filter('filtername', function($route, $request, $value)
{
if ($route == 'country') {
return Redirect::to(url);
}
});

Laravel 4 dynamic route with multiple segments

I would like to create a dynamic route that allows multiple segments.
example.com/segment1
example.com/segment1/segment2/
example.com/segment1/segment2/segment3
Using this:
Route::get('{slug}', 'PagesController#index');
Gives me my first dynamic segment and of course I could do this:
Route::get('{slug1}/{slug2}', 'PagesController#index');
and so on but I'm sure there's a better way to do this.
I believe one complication when using Route::get('{slug1}/{slug2}', 'PagesController#index'); would be handling all possible inputs so segment1/segment2/ and also foo/bar/. You would probably end up with lots of unnecessary logic.
This may not be the best solution but I believe groups would work well for what you are trying to accomplish.
Route::group(array('prefix' => 'segment1'), function() {
Route::get('/', 'ControllerOne#index');
Route::group(array('prefix' => 'segment2'), function() {
Route::get('/', 'ControllerTwo#index);
Route::get('/segment3', 'ControllerThree#index');
});
});
Maybe a little messy when only dealing with three examples but it could end up being beneficial and provide a nicer hierarchy to work with.
This also has the benefit for using before and after filters. Like for all segment2 endpoints if you want to perform some filter, instead of adding the filter to all the individual endpoints you can just add it to the group!
Route::group(array('before' => 'someFilter', 'prefix' => 'segment2'), function() {
// ... ...
});

Categories