首页 > 解决方案 > Laravel Controller request array empty for Route with Parameters

问题描述

I want to have an SEO friendly url's for this I made a jquery function where I visit the web page. like mentioned in blade.php.

The parameters are successfully passed in the url but I am not receiving it on controller side. and display empty or null .

index.blade.php

var v_href      = "http://mywebsite.com/program/course/"+discipline+"/"+city+"/"+discipline_id+"/"+city_id;
v_href          = v_href.replace('--', '-');
window.location.href = v_href;

Route file

web.php

Route::get('/program/course/{discipline?}/{city?}/{discipline_id?}/{city_id?}', 'FinderController@index')->name('finder.index');

Controller function


            $discipline_id  = $request->input('discipline_id');
            $city_id        = $request->input('city_id');

but I am getting null in both variables

标签: phpjquerylaravelseo

解决方案


要检索路由参数,您必须将它们作为参数添加到控制器函数。所以,在你的情况下,说函数被调用getSource(),你可以这样做:

public function getSource(?string $discipline = null, ?string $city = null, ?int $discipline_id = null, ?int $city_id = null)
{
    dd($discipline, $city, $discipline_id, $city_id);
}

推荐阅读