【发布时间】:2014-03-06 12:45:02
【问题描述】:
首先,让我们进入应用程序的上下文:
我们在 CustomerController 上,这是一个 Resource Controller 向 Store 方法发送一个 post 请求。
这是我的存储方法:
$customerDTO = new \repositories\dtos\CreateCustomerDTO();
$customerDTO->hydrate( Input::All() );
/** @var Customer $customer */
$customer = $this->customers->create( $customerDTO );
if ( $customer != null ){
header('Location: '.url('/customers/'.$customer->clacli));
return Redirect::action('CustomerController@show', array($customer->id) );
}
$userMessage = array(
'messageType' => 'error',
'messageContent' => 'Error creating a new Customer'
);
return Redirect::action('CustomerController@index')
->with(array('userMessage' => $userMessage));
我不得不添加一个“标题”,因为重定向调用不起作用。
当我把这行:
$customer = $this->customers->create( $customerDTO );
所有重定向都停止工作。
$this->customers 里面是什么?它只是一个从控制器中抽象数据库的存储库,我将需要在不久的将来更改数据库。
嗯,$this->customers->create 里面的代码是:
return Customer::create( $dto->toArray() );
它正在运行,我的 customersRepository 的所有测试也正在运行。这只是使用数据数组调用 Eloquent Model create 方法。
所以我无法弄清楚为什么重定向不起作用。有什么建议吗?
我尝试使用 return 'test';就在调用 $this->customers->create($customerDTO); 之后也没有工作。
【问题讨论】:
-
dd($customer) 在 $customer = $this->customers->create( $customerDTO ); 之后显示什么?
-
使用
Redirect::action的时候,是不是需要对应那个控制器的路由?这可能是我没有使用过的一种重定向类型,所以我不是 100%。 -
对象(客户)#325 (20) ...等
-
不是重定向,而是响应。返回“文本”;也不行