Laravel 5.2 subdomain dynamical routes to controllers - php

Lets assume I have a site with cars: cars.com on Laravel 5.
I want to set up my routes.php so a user could type in a browser ford.cars.com/somethingOrnothing and get to the controller responsible for Ford™ cars (FordController).
Of course I could use something like this code:
Route::group(['middleware' => 'web'], function () {
Route::group(['domain' => 'ford.cars.com'], function(\Illuminate\Routing\Router $router) {
return $router->resource('/', 'FordController');
});
});
But I am not happy about writing and maintaining routes for hundreds of car brands.
I would like to write something like this:
Route::group(['domain' => '{brand}.cars.com'], function(\Illuminate\Routing\Router $router) {
return $router->get('/', function($brand) {
return Route::resource('/', $brand.'Controller');
});
});
So the question is: Is it possible to dynamically set routes for sub-domains and how to achieve this?
upd:
the desirable outcome is to have subdomains that completely repeat controllers structure. Like Route::controller() did (but it is now deprecated)

To emulate Route::controller() behaviour you could do this:
Route::group(['domain' => '{carbrand}.your.domain'], function () {
foreach (['get', 'post'] as $request_method) {
Route::$request_method(
'{action}/{one?}/{two?}/{three?}/{four?}',
function ($carbrand, $action, $one = null, $two = null, $three = null, $four = null) use ($request_method) {
$controller_classname = '\\App\\Http\\Controllers\\' . Str::title($carbrand).'Controller';
$action_name = $request_method . Str::title($action);
if ( ! class_exists($controller_classname) || ! method_exists($controller_classname, $action_name)) {
abort(404);
}
return App::make($controller_classname)->{$action_name}($one, $two, $three, $four);
}
);
}
});
This route group should go after all other routes, as it raises 404 Not found exception.

Probably this is what you need:
Sub-Domain Routing
Route groups may also be used to route wildcard sub-domains.
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) {
//
});
});
From:
Laravel 5.2 Documentation
upd.
If you want to call your controller method you can do it like this:
Route::group(['domain' => '{account}.myapp.com'], function () {
Route::get('user/{id}', function ($account, $id) {
$controllerName = $account . 'Controller' //...or any other Controller Name resolving logic goes here
app('App\Http\Controllers\\' . $controllerName)->controllerMethod‌​($id);
});
});

Related

Laravel subdomain routes for multiple entity types

I have a project which have multiple subdomains.
for example I have a subdomain for Students which goes to a student controller and it looks like this:
Route::domain('students.domain.test')->group(function () {
Route::get('/', function () {
return "done reaching the students page";
});
});
The second type of domains is "domain.test" and any subdomain which I'm checking in the request level and that's fine too.
Route::get('/', [HomeController::class, 'index'])->name('index');
But before the second type of domains I want to make subdomain for specific types of Entities which I have in the database.
Route::domain('{someTypes}.domain.test')
->group(function () {
Route::get('/', function () {
return "done reaching SomeTypes Page";
});
});
My Entity table have these attributes: Id, Title, Type "which I want to check if the type is 5".
I tried to use the middleware:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
return $next($request);
}
abort(404, 'Sorry, Request Not Found');
}
and I applied it to my routes like this:
Route::group([
'middleware' => ['type:journal']
],function () {
Route::get('/', function(){
return 'journals logic goes here';
});
});
and I have another middleware to ignore types like this:
public function handle($request, Closure $next, ...$types)
{
$currentEntity = app('current_entity');
if ($currentEntity->entityType()->whereIn('title->en', $types)->exists()) {
abort(404, 'Sorry, Request Not Found');
}
return $next($request);
}
and applied it to the other routes like this:
Route::group([
'middleware' => ['except_entity:journal']
], function(){
Route::get('/', function(){
return 'default pages when journals fails';
})->name('index');
I hope its clear what I'm trying to achieve.
First, you need a check what version laravel that you used?
You need to use Middleware. And I think, method to code with laravel 6, 7, or 8, is a little bit different.
Can you give us more information about your code, so we can help it easier?

Laravel 7 Binding Routes

I have this route declared on laravel:
Route::get('pages/{page}/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
This route works fine and works if you make requests to:
https://example.com/pages/1/test-page
https://example.com/pages/2/other-page
https://example.com/pages/3/url-test
The problem is that I need a more friendly url as well as.
https://example.com/test-page
https://example.com/other-page
https://example.com/url-test
I want remove the suffix called pages, The numbers for the pages will never change and will be static for each one.
I've tried to make static routes for each one but can't get it to work.
Route::get('other-page', array('as' => 'other-page', function() {
return App::make('Common\Pages\CustomPageController')->show(2);
}))->middleware(['web', 'prerenderIfCrawler']);
I would appreciate a little help.
You could always get the URL segment in the Controller and use that to know what page you are on. If you don't want to do that you could pass extra information in the 'action' to specify the page:
Route::middleware(['web', 'prerenderIfCrawler'])->group(function () {
Route::get('test-page', [
'uses' => 'Common\Pages\CustomPageController#show',
'page' => 'test-page',
]);
...
});
Then you can get this extra information in the Controller:
public function show(Request $request)
{
$page = $request->route()->getAction('page');
...
}
If you knew all the pages you can use a route parameter with a regex constraint to restrict it to only those page names:
Route::get('{page:slug}', ...)->where('page', 'test-page|other-page|...');
public function show(Page $page)
{
...
}
You could just make use of a wildcard to catch your routes like this:
Route::get('/{slug}', 'Common\Pages\CustomPageController#show')
->middleware(['web', 'prerenderIfCrawler']);
Then in your controller:
public function show($slug)
{
$page = Page::where('slug', $slug)->first();
// ...
}
Just be careful with where you place the route. It should be at the end of your routes otherwise it will catch all the request of your app.
// ...
// my other routes
// ...
Route::get('/{slug}', ...);
By the way, if you want to bind your page models using the slug attribute do this:
Route::get('/{page:slug}', 'Common\Pages\CustomPageController#show')->//...
^^^^^^^^^
Then in your controller:
public function show(Page $page)
{ ^^^^^^^^^^
// ...
}
Check this section of the docs.

Laravel Multi-language Route Prefix

I managed to get some routes working with and without a prefix. Having routes that do not have the prefix work properly is important as it's too much work to go back and change all get/post links to the correct localized ones.
For example with the code below the URL localhost/blog redirects to localhost/en/blog (or any other language stored in session).
However, I noticed that URLs with parameters don't work, so /blog/read/article-name will result in a 404 instead of redirecting to /en/blog/read/article-name.
Routes:
Route::group([
'prefix' => '{locale}',
'middleware' => 'locale'],
function() {
Route::get('blog', 'BlogController#index');
Route::get('blog/read/{article_permalink}', 'BlogController#view');
}
);
Middleware is responsible for the redirects which don't seem to fire at all for some routes as if the route group isn't matching the URL.
public function handle($request, Closure $next)
{
if ($request->method() === 'GET') {
$segment = $request->segment(1);
if (!in_array($segment, config('app.locales'))) {
$segments = $request->segments();
$fallback = session('locale') ?: config('app.fallback_locale');
$segments = array_prepend($segments, $fallback);
return redirect()->to(implode('/', $segments));
}
session(['locale' => $segment]);
app()->setLocale($segment);
}
return $next($request);
}

Laravel 5 conditional routing and multiple controllers

So basically my app has two types of dynamic url..
app.com/{page}
app.com/{user}
Both having their own controllers
PageController#index
User\ProfileController#index
But I'm struggling to get this working.
I have tried a few different methods. Here are two I have tried..
Route::get('{slug}', function($slug) {
if (App\Page::where('slug', $slug)->count()) {
// return redirect()->action('PageController#index', [$slug]);
// return App::make('App\Http\Controllers\PageController', [$slug])->index();
return 'Page found';
} else if (App\User::where('username', $slug)->count()) {
// return redirect()->action('User\ProfileController#index', [$slug]);
// return App::make('App\Http\Controllers\User\ProfileController', [$slug])->index();
return 'User found';
} else {
return abort(404);
}
});
I feel I should be doing this with middleware/filters. Any help would be great. Thanks.
I think you could achieve what you after with Route::group using a middleware to filter if it is a page or a user.
Route::group(['middleware' => 'isPage'], function () {
Route::get('{slug}', ['as'=> 'pages.show', 'uses' => 'PageController#show']);
});
Route::group(['middleware' => 'isUser'], function () {
Route::get('{slug}', ['as'=> 'users.show', 'uses' => 'User\ProfileController#show']);
});
If you were using slugs for the Pages and ids for the Users, your idea of handling the issue might make more sense, but since you are using slugs for both the pages and the users, I strongly suggest you try a different approach. Why not declare two routes? Why not use the "show" methods of the respective controllers while you are at it, and keep in line with conventions for resources?
Route::get('pages/{slug}', ['as'=> 'pages.show', 'uses' => 'PageController#show']);
Route::get('users/{slug}', ['as'=> 'users.show', 'uses' => 'User\ProfileController#show']);
And if you really want to keep your "root-slug-respective-redirect" functionality you could write afterwards:
Route::get('{slug}', function($slug) {
if (App\Page::where('slug', $slug)->count()) {
return redirect(route('pages.show', $slug));
} else if (App\User::where('username', $slug)->count()) {
return redirect(route('users.show', $slug));
}
return abort(404);
});
I do advise against it though, as it seems like a waste of queries.
Here are the docs on Laravel RESTful resource controllers for good measure.

Laravel Routing

In Laravel, I want to have two different routes that have the same URL, but that runs a different controller based upon the datatype of the input. For example:
Route::get('/name/{id}/', function($id)
{
return 'id is an int:' . $id;
})->where('id', '[0-9]+');
Route::get('/name/{id}/', function($id)
{
return 'id is a string: ' . $id;
})->where('id', '[a-z]+');
This doesn't seem to work, though - the second route seems to overwrite the first completely, so the app wouldn't support ids that were integers. How do you actually accomplish this in Laravel without doing the checking manually inside the route?
Thanks
To not overwrite the first route, use different parameter name
Route::get('/name/{id}/', function($id)
{
return 'id is an int:' . $id;
})->where('id', '[0-9]+');
Route::get('/name/{stringId}/', function($id)
{
return 'id is a string: ' . $id;
})->where('stringId', '[a-z]+');
I think you can seperate this two routing mechanish from each other.
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
})
->where('id', '[0-9]+');
This code sample from Laravel site. If you want seperate logic more than that you can use filter.
Filter sample:
Route::filter('foo', function()
{
if (Route::input('id') == 1)
{
//
}
});
I hope i can help you.

Categories