Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

osc2nuke's avatar

Struggling with a Route

Hi folks! I'm having a struggle with a Route ( not sure if i'm doing it right, and yes for now i want that cPath = XX_XXX_X, as it represents multi-level categories, having slugs how i want it will go become to complex in the start-up of this project):

Route::get('/cPath={cPath?}', [IndexController::class, 'index'])->name('home');

If i do in my IndexController index():

    public function index(Request $request)
    {
        $cPath = $request->key; //or any other option i read about
        ddd($cPath);
...

I always get null or false. if i do :

    public function index($cPath)
    {
        ddd($cPath);

I get the desired value, but only in the controller, not in any other class where i require that paremeter.. What am i missing? and is the Route itself even correct? I'm not sure, as i was hoping i could also use it for the main index, and only load specific other elements when the cPath is given with correct values. (3_17_26 for example)

0 likes
3 replies
Cakra's avatar

To define route parameters, you need to write it like this

Route::get('/cPath/{cPath}', [IndexController::class, 'index'])->name('home');

to accept/read the uri parameters you can simply write like this

public function index($cPath)
    {
       dd($cPath);
webrobert's avatar
Level 51

@osc2nuke

i think you want...

domain.com?cPath=cpathvalue

Route::get('/', [IndexController::class, 'index'])->name('home');

    public function index(Request $request)
    {
        dd( $request->input('cPath') )
	}

if you want

domain.com/cPath/cpathvalue

Route::get('/cPath/{cPath?}', [IndexController::class, 'index'])->name('home');
    public function index($cpath = null, Request $request)
    {
        dd($cpath)
	}

null because the ? makes it optional.

1 like
osc2nuke's avatar

@webrobert Thanks Robert! Now it is available everywhere i need it. I require the first option.

1 like

Please or to participate in this conversation.