N
N
nicolaa2021-07-01 18:39:26
Laravel
nicolaa, 2021-07-01 18:39:26

How to specify city in url?

There are multiple urls

Route::get('part/{slug}', [App\Http\Controllers\PartsController::class, 'part'])->name('part');
Route::get('parts', [App\Http\Controllers\PartsController::class, 'parts'])->name('parts');
Route::get('spare-cars/{marka?}/{model?}/{slug?}', [App\Http\Controllers\CarController::class, 'spareCars'])->name('spare.cars');

You need to specify the city before each url, for example

site.ru/ekaterinburg/part - the cities are specified dynamically, and in case there is no city, you need to substitute it automatically, for example

We go to site.ru/part - we redirect to the default value of the city - site.ru/moskva/part and similarly with other addresses

How can this be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nicolaa, 2021-07-01
@nicolaa

They suggested a solution to the problem on stackoverflow
We add all routes to the group and do processing if we entered without a city, I don’t know how correct this solution is, but at the moment this solution works

Route::prefix('{city?}')->group(function() {
    Route::get('part/{slug}', [App\Http\Controllers\PartsController::class, 'part'])->name('part');
    Route::get('parts', [App\Http\Controllers\PartsController::class, 'parts'])->name('parts');
    Route::get('spare-cars/{marka?}/{model?}/{slug?}', [App\Http\Controllers\CarController::class, 'spareCars'])->name('spare.cars');
    Route::get('/', [App\Http\Controllers\PartsController::class, 'checkUrl'])->name('checkUrl');
});


    public function checkUrl($slug) {
        if($slug == 'parts') {
            return redirect('moskva/parts');
        } else if($slug == 'part') {
            return redirect('moskva/part');
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question