CakePHP - 控制器还是无控制器? [英] CakePHP - Controller or No Controller?

查看:233
本文介绍了CakePHP - 控制器还是无控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建立一个网路应用程式,分别有两种模式:捐款模式和捐款模式。它有多个用户角色。当工作人员用户首先注册捐赠者时,我希望他被重定向到另一种形式,允许他填写捐赠细节(捐赠者在第一次捐赠成功后注册)。



总之,我应该创建一个捐赠控制器,我将使用以下方式重定向用户:

  return $ this-> redirect(array('controller'=>'donations','action'=>'add')); 

对于上面的工作,它要求我保存新注册的捐赠者的id在会话中:

  $ this-> Session-> write('id',$ this-> Donor-> id ); 

所以用户被重定向到donations / add在url,但我认为这有一些缺陷。我漫游是否应该在Donor控制器中创建另一个操作,称为add_donation,它将具有各自的View。这个想法是能够形成一个类型的url:'donor / add_donation / 4'(4是donor_id!)
这个URL遵循这个结构:'controller / action / id'

如果任何人能够阐明最佳实践,或者描述我的解决方案的任何警告(前者,使用会话等),请帮助一个兄弟!不深深地感激你!提前感谢!

解决方案

保存数据后,您可以在DonorsController中执行此操作:

  $ this-> redirect(array(
'controller'=>'donations',
'action'=> add',
$ this-> Donor-> getLastInsertId()
));

没有必要返回重定向,它是无用的,因为你被重定向。注意,我们将最后插入的记录id作为get param传递给重定向。 控制器调用的重定向方法默认情况下 _ stop()



CakePHP3:有关于在3.0中更改默认行为的讨论。看起来像在CakePHP 3.0的redirect()不会退出()默认情况下更多。



DonationsController:

  public function add($ donorId = null){
//如果你喜欢
,获取捐助者显示它$ if($ this-> request- > is('post')){
$ this-> request-> data ['Donation'] ['donor_id'] = $ donorId;
//保存代码在这里
}
}

不使用会话在这里,特别不是通过保存它一个完全无意义的通用值名为id。如果我一定使用总是有意义的名称和命名空间,例如Donor.lastInsertId作为会话密钥。



如果它们是相关的,根据经验,应该进入他们所属的域,这在这种情况下很清楚IMHO。



编辑:
如果有人需要,可以在此处离开此编辑 - 不符合asker的使用方案。
如果您在此阶段登录了用户,请修改添加函数以检查传递的userId是否与登录的相同:



DonationsController:

  public function add($ donorId = null){
//如果你想要
if($ this-> request-> is('post')){
if($ this-> Auth-> user('id')!= $ donorId){
throw new InvalidArgumentException();
}
$ this-> request-> data ['Donation'] ['donor_id'] = $ donorId;
//保存代码
}
}


I am currently building a web app which has two models, Donor and Donation Models respectively. It has multiple user roles. When the staff user first registers a donor, I want him to be redirected to another form which allows him to fill in the Donation details(the donor is registered once the first donation is successful).

Firs of all, should I create a donation controller, from which I would redirect the user using:

return $this->redirect(array('controller'=>'donations','action'=>'add'));

For the above to work, it requires me to save the newly registered donor's id in a session like so :

$this->Session->write('id', $this->Donor->id);

So the user is redirected to 'donations/add' in the url, and this works fine.. However I think this has some flaws. I was wandering whether I should create another action inside the Donor controller called 'add_donation', which will have its respective 'View'. The idea is to be able to form a url of the sort : 'donors/add_donation/4' (4 being the donor_id ! ) This URL follows this construct: 'controller/action/id'

If anyone could shed some light on best practices, or describe any caveats to my solution(the former, using session etc.) , please do help a brother out! Ill be deeply indebted to you! Thanks in advance!

解决方案

After you saved the data you can do this in the DonorsController:

$this->redirect(array(
   'controller' => 'donations',
   'action' => 'add',
   $this->Donor->getLastInsertId()
));

There is no need to return a redirect, it's useless because you get redirected. Notice that we pass the last inserted record id as get param in the redirect. The redirect method of the controller calls by default _stop() which calls exit().

CakePHP3: There is a discussion about changing that default behavior in 3.0. Looks like in CakePHP 3.0 the redirect() won't exit() by default any more.

DonationsController:

public function add($donorId = null) {
   // Get the donor to display it if you like to
   if ($this->request->is('post')) {
    $this->request->data['Donation']['donor_id'] = $donorId;
    // Save code here
   } 
}

I would not use the session here, specially not by saving it to a totally meaningless and generic value named "id". If at all I would use always meaningful names and namespaces, for example Donor.lastInsertId as session key.

It's not always clear where to put things if they're related but the rule of thumb goes that things should go into the domain they belong to, which is pretty clear in this case IMHO.

Edit: Leaving this edit here just if someone else needs it - it does not comply with the usage scenario of the asker. If you have the user logged in at this stage, modify the add function to check if the userId passed is the same as the one logged in:

DonationsController:

public function add($donorId = null) {
   // Get the donor to display it if you like to
   if ($this->request->is('post')) {
       if ($this->Auth->user('id') != $donorId) {
           throw new InvalidArgumentException();
       }
    $this->request->data['Donation']['donor_id'] = $donorId;
    // Save code here
   } 
}

这篇关于CakePHP - 控制器还是无控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆