What Is The Best Practice To Validate a Property of a Controller In an In another Controller Validation
-
12-12-2019 - |
Question
Let me explain a demo situation.
I need to allow only logged in customers to subscribe newsletter. So in newsletter controller, I am validating whether the user is guest or a valid customer. If the user is guest, I am redirecting the user to log-in page.
Now suppose user is logging into his account. In that case, during the success login, I need to automatically make newsletter subscription for that user. In order to do this operation, I need to get subscription details during the validation action takes place.
What is the best way to do this ?
These are my ideas.
1) Make a global object that holds newsletter details (using Mage::register()
)
2) Create a session that holds newsletter for future validation.
3) send newsletter details via url to login page and then use those data there
Note: I will most probably use an observer for validation. So I need to varify newsletter status over there :)
Thanks in advance for any kind of suggestion
Solution
We can do this by preDispatch
function of controllers. Magento triggers preDispatch
function before any controller action get dispatched.
So You need to add a new preDispatch()
on your controller and check whether customer is logged in or not. If logged in,then routers will go to Newsletter Save Action and If customer is not logged in, then router will redirect to Customer Log-in Action, after setting your request save action to session variable. This is how it saves to the session.
Mage::getSingleton('customer/session')->getBeforeUrl();
Description with an Example
step 1
Create a helper function that will allow us to save the subscription-save-url along with current url that is encoded.` That is,
public function getSaveUrl()
{
return $this->_getUrl(
'yourmodule/yourcotroller/yoursaveaction',
array(
'myflagnewsletter' => true,
Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => $this->getEncodedUrl()
));
}
step 2
After click on the link, you will redirect to your own controller and there, you will check whether customer is logged in or not with the help of predisPatch
function. Using this function, we will prevent redirection to the matching action (means subscriber save action)
public function preDispatch()
{
parent::preDispatch();
if (!Mage::getSingleton('customer/session')->authenticate($this)) {
$this->setFlag('', 'no-dispatch', true);
if(!Mage::getSingleton('customer/session')->getBeforeUrl()) {
Mage::getSingleton('customer/session')->setBeforeUrl($this->_getRefererUrl());
}
}
}
If Customer is not logged-in, then the request url get saved to BeforeUrl
property by default. This will then use to redirect the customer to subscriber-save-action after customer-login-action took place.
Step 3
After successfully logged-in, now Magento will redirect to subscriber controller and again checks predispatch
function ,`customer is logged or not .As now customer is logged in, it will fail condition inside the function and thus continue to the subscriber-save-action method. This should somewhat looks like this.
public function yoursaveactionAction()
{
$session = Mage::getSingleton('core/session');
$backUrl = $this->getRequest()->getParam(Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED);
$customerflag = (int) $this->getRequest()->getParam('myflagnewsletter');
if (!$backUrl || !$customerflag ) {
$this->_redirect('/');
return ;
}
if ($backUrl && !$customerflag) {
$session->addError($this->__('Not enough parameters.'));
$this->_redirectUrl($backUrl);
return ;
}
try {
// save you model
$session->addSuccess($this->__('Alert subscription has been saved.'));
}
catch (Exception $e) {
$session->addException($e, $this->__('Unable to update the alert subscription.'));
}
$this->_redirectReferer();
}
OTHER TIPS
If you use sessions or Mage::register
, then guests will not be able to subscribe after a certain time period. depends on session expiration limit. while if you want the customers to get subscribed whenever they login using the link you provide, then you can use the third option where you can append a variable like subscribe=1
and access it in the observer.