P
P
Prodion2021-03-28 00:07:59
Laravel
Prodion, 2021-03-28 00:07:59

How to design a route?

You need to achieve the following result:

/yarn/
/yarn/alize/
/yarn/alize/lana-gold

/yarn/
/yarn/{manufacturer}/
/yarn/{manufacturer}/{collection}

I did this:
Controller
class YarnController extends Controller
{
    public function index()
    {
        return 'index()';
    }

    public function show($manufacturer, $collection = null)
    {
        if ($collection) {
            $test = $manufacturer .', '. $collection;
        } else {
            $test = $manufacturer;
        }

        return $test;
    }
}

route
Route::group(['prefix' => 'yarn'], function() {
    Route::get('/', [YarnController::class, 'index']);
    Route::get('/{manufacturer}/{collection?}', [YarnController::class, 'show']);
});

I am afraid that in the future it will be necessary to collide with any pitfalls. I started doing it several times and after N days I had to redo everything due to a wrong start. I hope senior colleagues will help.

If in the future I want to list yarns not only by producer, but also by country, then I will definitely run into difficulties.
// /yarn/{country}
I looked at different working sites and in some way they implemented this logic, but I don’t understand exactly how.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question