【问题标题】:Laravel 4: Redirect a post request to different controller methodLaravel 4:将发布请求重定向到不同的控制器方法
【发布时间】:2014-11-05 21:28:01
【问题描述】:

我有一个像下面这样的控制器,
MyController:

public function methodA() {
    return Input::get('n')*10;
}  

public function methodB() {
    return Input::get('n')*20;
}  

我想根据POST的值调用MyController里面的a方法。

routes.php

Route::post('/', function(){
    $flag = Input::get('flag');
    if($flag == 1) {
        //execute methodA and return the value
    } else {
        //execute methodB and return the value
    }
});

我该怎么做?

【问题讨论】:

  • 您可以在Route 之后使用双冒号作为开头:D
  • 只是一个疯狂的建议,为什么不使用额外的 url 参数来省去这个麻烦
  • 你的意思是根据url值(即通过POST汇总)还是根据$_POST参数值?
  • @Yani 根据 $_POST 参数
  • @palatok 好的,请在下面查看我的答案。应该工作!

标签: php laravel-4 laravel-routing


【解决方案1】:

这是针对 Laravel 4.x 的。使用Laravel 5时,需要添加Namespaces...问题是关于Laravel 4的


Route::controller() 方法正是您所需要的。

您的路线文件应如下所示:

Route:post('/', function(){
    $flag = Input::get('flag');
    if($flag == 1) {
        Route::controller('/', 'MyController@methodA');
    } else {
        Route::controller('/', 'MyController@methodB');
    }
});

这些方法看起来像这样:

public function methodA() {
    return Input::get('n') * 10;
}  

public function methodB() {
    return Input::get('n') * 20;
}  

【讨论】:

  • 我收到 ReflectionException (-1) 类不存在
  • 这适用于 Laravel 4.x。使用Laravel 5时,需要添加Namespaces...问题是关于Laravel 4
【解决方案2】:

我认为更简洁的解决方案是根据您的标志将您的发布请求发送到不同的 URL,并为每个 URL 设置不同的路由,映射到您的控制器方法

Route::post('/flag', 'MyController@methodA');
Route::post('/', 'MyController@methodB);

编辑:

要按照自己的方式进行操作,您可以使用这个 sn-p

Route:post('/', function(){
    $app = app();
    $controller = $app->make('MyController');
    $flag = Input::get('flag');
    if($flag == 1) {
        return $controller->callAction('methodA', $parameters = array());
    } else {
        return $controller->callAction('methodB', $parameters = array());
    }
});

Source

Route:post('/', function(){
    $flag = Input::get('flag');
    if($flag == 1) {
        App::make('MyController')->methodA();
    } else {
        App::make('MyController')->methodB();
    }
});

Source

请注意 - 我对 Laravel 的实践经验绝对为零,我只是搜索并找到了这个。

【讨论】:

  • 我被要求以其他方式进行
【解决方案3】:

根据您在 cmets 中的回答,您需要 1 个 url 并根据 $_POST 值决定使用哪种方法。这就是你需要的:

在您的 Routes.php 文件中,添加一个通用方法

Route::post('/', 'MyController@landingMethod);

在您的MyController 文件中:

public function landingMethod() {
    $flag = Input::get('flag');
    return $flag == 1 ? $this->methodA() : $this->methodB();//just a cleaner way than doing `if...else` to my taste
} 

public function methodA() { //can also be private/protected method if you're not calling it directly
    return Input::get('n') * 10;
}  

public function methodB() {//can also be private/protected method if you're not calling it directly
    return Input::get('n') * 20;
}  

希望这会有所帮助!

【讨论】:

  • 不错的解决方案。但实际上我有多个控制器,我不认为从另一个控制器调用一个控制器不是那么漂亮。无论如何感谢您的回复
猜你喜欢
  • 2015-03-30
  • 2016-04-04
  • 2014-03-09
  • 2014-05-12
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 2019-08-06
  • 2020-02-01
相关资源
最近更新 更多