Support the ongoing development of Laravel.io →
posted 11 years ago
Requests

Hello,

For the development of an application there are several requirements regarding routing, which I can't seem to get to work properly in Laravel 4.1.

Subdomains

First, there is the issue of sub domains (solved, see below). Certain modules within the application are to be accessed via dedicated (fixed) sub domains, e.g. api.example.com. It was no problem to create routes for them:

Route::group(array('domain' => 'api.example.com'), function() {
	Route::get('/', function() {
		return 'API';
	});
});

However, a second requirement is that accounts are identified via a flexible sub-domain. So I added a route group for it:

Route::pattern('account', '[a-z][a-z0-9]+');
Route::group(array('domain' => '{account}.example.com'), function($account) {
	Route::get('/', function() {
		return 'Account: '.$account;
	});
});

This does the job, but it hijacks the previously defined route groups for api in certain circumstances

Consider the following:

Route::group(array('domain' => 'api.example.com'), function() {
	return 'API';
});

Route::pattern('account', '[a-z][a-z0-9]+');
App::before(function($request) {
	Route::group(array('domain' => '{account}.example.com'), function($account) {

		//Bar
		Route::any('bar', function() {
			return 'Bar';
		});
	});
});

This now has api.example.com redirecting properly to the API route group, but when you try to access api.example.com/bar, it takes you to the Bar route in the account group. This is not as intended, I want the domain route of API to have absolute priority over the account group, and api.example.com/bar should result in a not found exception.

Question #1: is there a way to group ALL requests on the sub domain api.example.com to the API routes group, while maintaining a routes group for accounts for all other variable sub domains?

Optional parameters

Second issue, I want to create some completely optional parameters. I assumed this would be the way to go:

Route::pattern('account', 		'[a-z][a-z0-9]+');
Route::pattern('i', 			'^i$');
Route::pattern('identity', 		'[0-9]+');
Route::group(array('domain' => '{account}.example.com'), function($account) {

	//Identity
	Route::group('{i?}/{identity?}', function($account, $i = null, $identity = null) {
	
		//Test route
		Route::any('test', function($account, $i = null, $identity = null) {
			return 'Account: '.$account.', Identity: '.$identity;
		});
	});
});

Accessing this via:

foo.example.com/i/2/test

works as expected, but accessing it via:

foo.example.com/test

results in a 404 exception. It gets better though, replace 'test' with '/' in the route and suddenly both of these requests work as expected:

foo.example.com/i/2

foo.example.com

Question #2: what is the best way to implement a set of two optional parameters into the URL which can be filtered out, without having to duplicate routes or resort to ugly hacks. In other words, I want both of the following requests to point to the same set of routes:

foo.example.com/any/request/here

foo.example.com/i/numeric/any/request/here

Maybe I'm just missing something obvious here, but I've gone through the guide on routing five times and exhausted Google and can't find the solutions. Any help or suggestions would be appreciated before I resort to hacking the router class.

Thanks!

Last updated 3 years ago.
0

Issue 1 is solved (more or less) with the following solution:

Route::group(array('domain' => 'api.example.com'), function() {
	Route::get('/', function() {
		return 'API 1';
	});
	Route::get('test', function() {
		return 'API test';
	});
	Route::any('{all}', function($uri) {
	    App::abort(404);
	})->where('all', '.*');
});

This implements a sort of catch-all route and seems to take priority over the accounts route group.

Last updated 3 years ago.
0

Issue 2 is now also solved using this solution:

//Check for identity segments
if (Request::segment(1) == 'i' and $identity = (int) Request::segment(2)) {
	//Do something with $identity
	$prefix = 'i/'.$identity;
}
else {
	$prefix = null;
}

//Wrapper for (optional) identity prefix
Route::group(array('prefix' => $prefix), function() {

	//Other route groups and routes defined here as usual
});

This way, if there is an identity parameter present, you can filter it out before the routes and set it as a prefix in a wrapper group. If no parameter is given, the prefix is null and is basically ignored.

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.