Why not using subdomains? Because in my opinion there is no way to define the parameter as optional.
Maybe try to make 2 routes, somewhat similar to Java's overloading methods.
Route::get('/{category}/{title}', function ($category, $title = '') {
dd('test');
});
Route::get('{languageCode}/{category}/{title}', function ($languageCode = 'pl', $category, $title = '') {
dd('test');
})
->where('languageCode', '[a-z]{2}');
EDIT: If you are using Controllers, you could try ...
// routes.php
Route::get('/{category}/{title}', 'SomeController@index');
Route::get('{languageCode}/{category}/{title}', 'SomeController@index')->where('languageCode', '[a-z]{2}');
// SomeController.php
class SomeController extends BaseController {
public function index()
{
$args = func_get_args(); // $args will contain all arguments
$n = func_num_args();
switch ($n) {
case 2:
$category = $args[0];
$title = $args[1];
// .. code ..
break;
case 3:
default:
$languageCode = $args[0];
$category = $args[1];
$title = $args[2];
// .. code..
break;
}
}
}
IMHO using route group with prefix is much cleaner
$locale = ...
Route::group(array('prefix' => $locale), function(){
// your routes here
});
$locale can be null - in this case use default locale.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community