Php laravel路径:变量的更改顺序
我试图在控制器中添加两个不同的路径来执行相同的操作,但是我需要在中间提供空值。 路线:Php laravel路径:变量的更改顺序,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我试图在控制器中添加两个不同的路径来执行相同的操作,但是我需要在中间提供空值。 路线: Route::get('post/get-ajax-posts/start/{start}/profile/{profileid}', 'PostController@getAjaxPosts'); Route::get('post/get-ajax-posts/start/{start}/tag/{tagName}', 'PostController@getAjaxPosts'); 控制器: pu
Route::get('post/get-ajax-posts/start/{start}/profile/{profileid}', 'PostController@getAjaxPosts');
Route::get('post/get-ajax-posts/start/{start}/tag/{tagName}', 'PostController@getAjaxPosts');
控制器:
public function getAjaxPosts(Request $request, $startRow, $profile = null, $tagName = null){}
$routeParameters = $this->getRouter()->getCurrentRoute()->parameters();
if(isset($routeParameters['tagName']) && $routeParameters['tagName']){
$tagName = $routeParameters['tagName'];
$profile = null;
}
我想在第二个路由中提供$profile=null,但我得到$profile={tagName}
我该怎么做
我唯一发现的是在控制器中添加:
public function getAjaxPosts(Request $request, $startRow, $profile = null, $tagName = null){}
$routeParameters = $this->getRouter()->getCurrentRoute()->parameters();
if(isset($routeParameters['tagName']) && $routeParameters['tagName']){
$tagName = $routeParameters['tagName'];
$profile = null;
}
有更简单的方法吗?很简单,兄弟
在公共函数中获取所有路由变量
public function getAjaxPosts(Request $request){
$startRow = $request['startRow'];
$profile = null ;
$tagName = null ;
}
很简单,兄弟
在公共函数中获取所有路由变量
public function getAjaxPosts(Request $request){
$startRow = $request['startRow'];
$profile = null ;
$tagName = null ;
}
您可以对可选参数使用
?
,例如'user/{name?}
imho它们看起来是两个不同的路由,具有两个不同的函数调用。如果逻辑相同,则可以使用一个函数检索参数并调用另一个函数。我不认为可选参数是一个适用于您编写路由的解决方案。您能否发布您的整个控制器方法,因为可能有一种更干净的方法来实现您的目标?另外,您是否必须使用相同的路由,即添加到现有代码中?当然,我可以用几种不同的方法来完成,也许我会这样做,但问题是,有没有一种解决方案可以在路由中插入此空值?您可以使用?
作为可选参数,如'user/{name?}“
imho他们似乎有两条不同的路线,有两个不同的函数调用。如果逻辑相同,则可以使用一个函数检索参数并调用另一个函数。我不认为可选参数是一个适用于您编写路由的解决方案。您能否发布您的整个控制器方法,因为可能有一种更干净的方法来实现您的目标?另外,您是否必须使用相同的路由,即您正在添加到现有代码中?当然,我可以用几种不同的方法来完成,也许我会这样做,但问题是,有没有一种解决方案可以将此空值注入路由中?POST方法是这样工作的,对于GET,您必须调用$routeParameters=$this->getRouter()->getCurrentRoute()->参数()代码>POST方法是这样工作的,对于GET,您必须调用$routeParameters=$this->getRouter()->getCurrentRoute()->parameters()代码>