Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0

Why not using subdomains? Because in my opinion there is no way to define the parameter as optional.

Last updated 2 years ago.
0
Solution

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;
     }
  } 
}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Uriziel01 uriziel01 Joined 1 Oct 2014

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.

© 2024 Laravel.io - All rights reserved.