Laravel routes with domains and subdomains - laravel

I've an app developed with laravel 5 and I will deploy it in a digitalocean droplet.
Can I have multiple domains and subdmanins in my server that using this unique application?
How I should implement routes.php to involve that?
I use this code to subdomains on 'mysite.com'
Route::group(array('domain' => '{subdomain}.mysite.com'), function() {
Route::get('customer/{customerId}', function() {
//blablabla;
});
});
But What I have to do if I have others domains like 'othersite.com'?
PS: All domains are hosted in the same droplet.

For subdomain, no problem (like said in http://laravel.com/docs/5.1/routing#route-group-sub-domain-routing)
For domain, you can, but with the url set in the config you'll have problem with link...
So, i'll do things like that :
Get the domaine name ( something like $request->server->get('SERVER_NAME') or request()->server->get('SERVER_NAME') )
update the config with \Config::set('app.url', $mydomainename)
You can do that in route, but maybe it will be better in a middleware.
And voila
Ps: take care about duplicate content with google, maybe some canonical will be usefull

Related

Modify the URL generated with Route::apiResource without changing the name

I'm building a website where users can post ads : a VueJS app that requests routes on an Laravel API.
I have an AdController, with an Ad model, and my routing is done via stuff like :
Route::apiResource('ads', AdController::class)->only(['update', 'destroy']);
Route::apiResource('ads.photos', AdPhotoController::class)->only(['index']);
which generates routes like PUT "/ads/{ad}" or GET "/ads/{id}/photos"....
This works very well, and my VueJS app uses Ziggy to call the API by their route name
axios.get(route('ads.photos.index', id))
And... It still works flawlessly ! No problem at all, and I have a LOT of routes with a LOT of API calls.
Now my problem : we realised that URLs containing "ads" are blocked by adblockers. That completely shuts down all access to our website, and asking users to turn off the adblocker is NOT a solution.
I could change my routes to do something like
Route::apiResource('posts.photos', AdPhotoController::class)->only(['index']);
but I have a LOT of routes and I really don't want to rename everything, everywhere.
Is there an option to change apiResources generated URL, so 'ads.photos.index' would generate "/posts/{id}/photos" instead of "/ads/{id}/photos" ?

How to set up a Laravel App with Subdomains to work on a cPanel server?

First of all: this is not a duplicate of (How to set up a laravel project on cPanel subdomain?) this question isn't the same as mine.
I would like to know how can i set up a whole Laravel application, with subdomains (not only one subdomain) to work on a cPanel server
So here is the test code i'm using on my routes/web.php file.
Route::domain('test.mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN TEST.MYDOMAIN.COM';
});
});
Route::domain('mydomain.com')->group(function() {
Route::get('/', function() {
return 'DOMAIN MYDOMAIN.COM';
});
});
Route::get('/', function() {
return 'ROOT DOMAIN';
});
I'm currently using cPanel to handle domains and subdomains, but i have no idea how to make it work with Laravel Subdomains, i tried the code above.
When i access my main domain mydomain.com it shows "DOMAIN MYDOMAIN.COM" so it works ok, but when i go to test.mydomain.com it just access my subdomain folders from this subdomain, so, how can i make it works?
Maybe i need to put some .htaccess file to get this working properly? Can someone help?
Did you try to link subdomains to the same folder as primary domain ?
Eg.
domain.com => /public_html/public
test.domain.com => /public_html/public

Problem with Laravel routes - all sub folder traffic ending up in route view

Sorry - was difficult to give this one a clear title! But I have an issue and a difference between how my local laravel install is dealing with some routes compared to my live server.
Locally, I have this working:
Route::get('/blog', 'BlogController#home');
Route::get('/blog/{post_slug}', 'BlogController#viewPost');
As you can probably guess, I want to serve up a list of posts via the home() function if /blog is hit. Then all other traffic with a "slug" after /blog/, I want to load the blog post.
This all works locally.
However on live,
/blog/my-blog-post
Is serving up the home() function every time.
Where would I start with debugging this. Laravel versions? Server caching?
Maybe you can do this in laravel 5.7+
Route::prefix('blog')->group(function () {
Route::get('/', 'BlogController#home');
Route::get('/{post_slug}', 'BlogController#viewPost');
});
before just use: php artisan optimize, to clear all cache route and config.
for more info see the docs

Laravel subdomain accepts other similar subdomain which are not even pointed to the project

I have pointed some subdomains to my project in laravel, some similar subdomain exists on the same server, but pointed to other projects.
Still sometimes they point to my project.
How to resolve this issue?
route structure-
I have a wildcard route-
Route::group(array('domain' => '{subdomain}.website.com'), function () {
Route::get('/', function ($subdomain) {
});
});
My subdomain which are pointed to this project are-
Xyz.website.com, abc.website.com
but other links like feh.website.com which are pointed to other projects are sometimes pointing to my project.
Please help for above scenario .
I thought it depends on your web server config.
If you use nginx as web server, you should declare the server name:
{
...
servername xyz.website.com abc.website.com
...
}
If you get some else url point to your project like feh.website.com, that maybe because you did not declare settings for feh.website.com and it will go to your first virtual server.

How can I make a dynamic route in Laravel 5.2

I need make a dynamic route or dynamic subdomain for multitenant site. I am not sure the better option. The matter is when the user create the account should get a URI like:
http://www.domain.dom/username
or
http://username.domain.com
I am not expert so I prefer the easiest way to do that. Any idea?
Thanks a lot
You can set up your routes like this:
// First example
Route::get('{username}', 'MyController#myAction');
// Second example
Route::group(['domain' => '{username}.domain.com'], function() {
Route::get('/', 'MyController#myAction');
});
Please note that you still have to set up your webserver to listen to all urls.
More info on subdomain routing can be found here: https://laravel.com/docs/5.2/routing#route-group-sub-domain-routing

Resources