How can I make a dynamic route in Laravel 5.2 - laravel

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

Related

Laravel route works with subdomain in 2 environments

I'm coding a platform to generate sub-websites.
I have a route like this who works very well in local :
//Website
Route::domain('{slug}.domain.test')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
I want to be able to make it works as well in production (other domain), so i did :
//Website
Route::domain('{slug}.{domain}')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
And in my template :
Website
The generated URL looks amazing, but the routing doesn't work and bring me to the parent page of the main domain.
What i am doing wrong ?
Thanks
Working with domain routes like this is a little bit of a pain in Laravel.
In an application recently, I parsed the domain part from the application URL and then set it as a configuration value like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([
'app.domain' => parse_url(config('app.url'), PHP_URL_HOST),
]);
}
}
You can then use that in your domain routes:
Route::domain('{slug}.'.config('app.domain'), function () {
// Subdomain routes that work in all environments
});
If you are using laravel homestead, you'll need to register every subdomain on the etc/hosts file and on Homestead.yaml, as you probably did with the main domain. Now, I'll recommend the structure:
app.test
subdomain1.app.test
subdomain2.app.test
I wouldn't recommend to use something like:
subdomain1.app1.test
subdomain2.app2.test
I mean, you could, but it doesn't make much sense, and you would also have to register all of this on your Homestead/Local and Production environments.
For the approach that I suggest, you could set up this on an environment variable. https://laravel.com/docs/6.x/configuration#environment-variable-types
You can add any env variable you want/need to the .env file, there's an APP_URL variable, but this includes the http protocol, you could add something like:
APP_DOMAIN=app.test
And for production
APP_DOMAIN=yourapp.com
Then on the routes file access it with the helper method env, you can omit the second parameter, or use it to setup a default value, in case you forget to put it on the .env file.
Route::domain('{slug}.' . env('APP_DOMAIN', 'default.com'))->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
BTW, this might be of help for setting up your slug value with named routes:
Setting up named routes within a subdomain group in Laravel 5.7

Laravel Passport Password Reset API route

I'm all set up with Passport in 5.5 and have the auto generated Auth\ForgotPasswordController and Auth\ResetPasswordController controllers.
However whereas /oauth/token was provided magically for me, there don't appear to be such routes for password reset when using the API.
What should my API routes look like?
Currently I've experimented with
Route::group(['prefix' => 'password'], function () {
Route::post('/email', 'Auth\ForgotPasswordController#sendResetLinkEmail');
Route::post('/reset', 'Auth\ResetPasswordController#reset');
});
but I found these in the vendor files when looking at the traits and aren't sure if this is the correct way.
The /password/email route also fails with "message": "Route [password.reset] not defined."
since you don't see any route other then 2 custom, therefore i am assumin you havn't run artisan auth command. First run that. it will add lot of routes in ur project.
Then set api driver to passport.

Vue SPA url not working with history mode enabled

I am creating a SPA using Vue js and it kind of works but I have one problem. With history mode enabled on Vue I can enter urls and go to that page using the Vue Router but when I try to login I get the literal page html. I know I can do something like auth\{vue?} but I would prefer if i didn't have to do that. I want to be able to always keep the url with no prefixes unless it is to an API request. So for example:
I have the root view:
Route::get('/{vue?}', function () {
return view('layouts.app');
})->where('vue', '[\/\w\.-]*');
and then I have the api requests:
Route::group(['prefix' => 'api'], function () {
Route::post('/login', 'Auth\\LoginController#login');
Route::post('/register', 'Auth\\RegisterController#register');
Route::get('/logout', 'Auth\\LoginController#logout');
});
but when I hit /api/login I get the return view data from /{vue?}.
I hope that makes sense and if so any help would be amazing, Thanks.
it is clearly explained here: https://kjamesy.london/work/laravel-53-vuejs-20-make-vuerouters-history-mode-play-nicely-with-laravels-routes
Short story: declare history mode to false in VueRouter and then on the Laravel side, whenever the $request->ajax() is false, the same route will always capture the request. So, we can safely define a catch-all route to redirect such traffic to the index() method of our resource controller:

Laravel routes with domains and subdomains

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

How to associate a route to a package?

Can i do something like Symfony to associate a route to a package?
So every route on my routes.php admin package are prefixed with '/admin/'?
Because actually i can put a route on my main routes.php like '/admin' and i can put the same on my package routes without knowing that on my main routes this route is already in use.
My current package routes.php is something like this:
Route::get('/admin', 'MyVendor\administration\AdminController#test');
But what happens if i put the same '/admin' on the main routes? i dont like this approach.
Sorry for my English, i hope you understand me.
Thank you
Yes you can (from laravel site)
Route::group(array('prefix' => 'admin'), function()
{
Route::get('user', function()
{
//
});
});
Read the documentation about Route Prefixing and this answer too.

Resources