开发者

General MVC Questions on PHP & Magento Validations

开发者 https://www.devze.com 2023-01-30 20:37 出处:网络
I have been using Magento & PHP CodeIgniter MVC for about 4 months. I am having a peculiar problem understanding where to set the validation logic for both CodeIgniter & Magento, only if I con

I have been using Magento & PHP CodeIgniter MVC for about 4 months. I am having a peculiar problem understanding where to set the validation logic for both CodeIgniter & Magento, only if I consider to follow the MVC architecture to its true nature & in a very proper semantic way.

First of all, I will start off with Magento:-

Say I have a "Assignment" module in Magento, which is about Players & Tournaments both. I have 3 action methods, in this "Assignment" module's controller, where I need to check & validate the following:-

  1. Player ID
  2. Tournament ID

This validation is also required in the "Assignment" module's Block class, along with in the 3 action methods.

So my query is should I write the validations for the above 2 points in this module's Controller class or in the Model class, because I will need to use the validations both in the Block class & also in the Controller class?

Also what does actual MVC architecture suggest in this case?

If possible, please provide a code snippet, highlighting the logic of your implementation.

Regarding CodeIgniter or any other PHP MVC Framework:-

Since Validation of anything using sessions is quite page-specific & since there is no concept of Blocks in CodeIgniter, so normally session validations & setting of session variables are done in the Controller class only.

So my query in this regard is that is this a correct approach & under what circumstances should I put setters of sessions & session validations in the Model class?

And again, what does actual MVC architecture suggest in this case?

And again, if possible, please provide a code snippet, highlighting the logic of your implementation.

I am really in confusion state regarding this & I'm sort of stuck in here. Please help me in here. Any solution / help is greatly appreciated. Many thanks in advance.

EDIT:-

If possible, please provide some Mag开发者_JS百科ento code on how to create & write proper Validations, along with throwing Exceptions, with the actual Exception messages defined in the Model methods?


The Model layer should be able to maintain its own consistency, so regardless of other decisions, you should include your validations in the Model layer. To help the user (and provide more helpful validation messages), you may also wish to do some validation in the controller level.

The advantage of this approach is that, assuming you maintain your model layer, there is no chance of a rogue controller setting bad data. This plays into your second question, for which the Magento answer would be to use an object to manage data in parts of the session, and to validate that data on the way into the session.


As an aside, to deal with validations in your Blocks/Views, consider using validation like this. It has its own flaws, but generally minimizes the amount of validation code you have to write:

// make sure that the below returns the relevant assignment model class
$assignment = $this->getAssignment(); // or get it via a session, or helper, or what have you.
$player = getChosenPlayer();


try {
    $assignment->setPlayer($player); //throws exception when invalid
    ... do more ...
    $assignmnent->save();
} catch(SomeException $e) {
    addValidationError($e->getMessage());
    renderPageAgain();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号