Support the ongoing development of Laravel.io →
Requests
Last updated 3 months ago.
0

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

Last updated 3 months ago.
0

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}', '[email protected]');
Route::get('{languageCode}/{category}/{title}', '[email protected]')->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 3 months 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 3 months ago.
0

Sign in to participate in this thread!

LoadForge

Your banner here too?

Uriziel01 Uriziel01 uriziel01 Joined 1 Oct 2014

Moderators

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

Your logo here?

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

© 2022 Laravel.io - All rights reserved.