Magento Redirect from Observer - magento

I am having trouble to create a working redirect in Magento from an observer.Apart from that I need to understand why the exception just like we do in controller does not work in Observer.
The typical exception done in controller is like below (adminhtml controller)
$message = $this->__('Exception Message.');
Mage::getSingleton('adminhtml/session')->addError($message);
$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
return;
Somewhere in the blog I read about the below method to redirect from observer.
Mage::getSingleton('core/session')->addError('Exception Message.');
Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getUrl('checkout/cart'));
Mage::app()->getResponse()->sendResponse();
exit;
I don't understand the basic redirection difference when doing with an observer and controller.
Why controller redirection does not work when used in observer.
Please help me out and explain.
Thanks a lot in advance.

See below link and i also have posted code
it might help you.
Source : http://aaronbonner.io/post/418012530/redirects-in-magento-controllers
Redirects in Magento Controllers
In Zend Framework controllers, to output something other than html you will want to disable ViewHelper defaults along with some of the other magic ZF typically weaves.
In Magento, the same thing applies when doing a controller redirect.
I noticed on PHP 5.2 a redirect seemed to be ignored whereas my Macports 5.3 setup it worked. Turns out I was missing a trick, and so a proper redirect in Magento is done as follows:
In MyPackage/MyModule/controllers/MyController.php :
$this->_redirectUrl($this->getUrl('http://www.someurl.com/someresource'));
$this->setFlag('', self::FLAG_NO_DISPATCH, true);
return $this;
Without the setFlag call, your redirect will be ignored. The return call stops any further code execution and sees your redirect get actioned.

Related

Laravel 5 -- Redirect in Controller is not working in AJAX call

Important: this comes from ajax call.
Everything works perfectly except:
use Illuminate\Routing\Redirector;
public function my_call() {
return redirect()->route('page-1');
}
Throws 500 error.
return view('page-1') works without problems.
Maybe anybody sees what I am doing wrong?
Thank you.
Do you have an actual route with the name that you are using, you cannot redirect to a view as these are returned by routes
Apperantly you cannot do this on server side if you do AJAX call:/
Here an explanation: https://laracasts.com/discuss/channels/vue/redirect-after-ajax-post-request
It's for vue but I believe it applies universally for ajax.

Laravel 4: Redirecting when invalid route

I am trying to treat the invalid requests on my Laravel app, something like redirecting to the root will work just fine, but I can't manage to do it.
In the documentation and around stackoverflow I saw this is always the solution:
App::missing(function() {
# handle the error
});
I thought that would just go to the routes file but no. Then I saw in some post it should be in the app/start/global.php file but it still didn't work.
In the docs it says I can "register an error handle". Is that what I should do? What does this mean? What should I do?
Ultimately this can be put anywhere, but app/start/global.php is probably the best place for it.
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});
Try putting this in there. In fact, if you've just setup a fresh installation, you should already have one there.
Here is how you can register an error handler, like so;
App::error(function(Exception $exception, $code)
{
//Now you can check for different exceptions and handle each on their own way.
}
This will be for PHP general Exception class which will make all exceptions go here, but you can change Exception to the specific Exception class of you own and handle it accordingly.
App::missing will generally be called when a page within your site has not been found, allowing you to show a default page for when users have found a non existing page. So if you are wanting to handle missing pages, use App::missing else use App::error.

does this codeigniter code do a redirect or return html

I was looking at the example http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#validationrules (trying to learn codeigniter) and saw this code
$this->load->view('myform');
My question is does this cause the server to send a redirect back to the user? or does this return the user html code?
ie. I am looking to be completely REST based where a post should always redirect and never should return html(plus returning html on post screws up browsers and we have noticed the playframework model of post -> redirect -> get to work 100% of the time with browser back buttons and behaving the way a user would expect and want to keep that in place)
If the above does not redirect, anyone know of an example with validation failing that does a redirect and then displays the errors.
thanks,
Dean
$this->load->view('myform'); is a function that parse the html resource and output to the browser.
there is a redirect function in url_helper can do that.
But i just imply my function.
redirect is showing a message then redirect.
jump is jumping without a message.
message is showing a note message.
public function redirect($url,$message,$time=3){
$this->template->display('common/redirect',array('url'=>$url,'message'=>$message,'time'=>$time),$this->theme,$this->thcolor,false,0);
}
public function jump($url){
$this->template->content_display("<html><head><meta http-equiv='refresh' content='0;url={$url}'></head><body></body></html>");
}
public function message($heading,$message){
$this->template->display('common/message',array('heading'=>$heading,'message'=>$message),$this->theme,$this->thcolor,false,0);
}
It loads HTML . As a general advice I would stay away from CI. It has tons of security bugs and it's not suited for your purpose. Try laravel or better rails

Zend Framework non-existent controller redirects to home page?

When I type an invalid url (non-existent controller), it displays the homepage rather than return a 404 Page Not Found page. Does anyone know the possible reason?
Thanks
Have checked if there is any plugins are registered in the bootstrap and if is it configured to catch the exceptions or set any ACL mechanisms.
Also check the error controller too. may be there is some forwarding methods implemented there.
Hi i've had the same problem and i now know how to fix it:
within the bootstrap have the following init
function _initModules()
{
$this->bootstrap('frontController') ;
$front = $this->getResource('frontController') ;
$front->addModuleDirectory(APPLICATION_PATH . "/modules");
$front->setParam("useDefaultControllerAlways", false);
}
Setting 'useDefault....' will use the default controller when a 404 occurs.
You should also make sure your error controller is setup correctly

CakePHP redirect method not redirecting

I am trying to redirect to a new page using CakePHP redirect method but for some reason the redirect is not working. The code I am using to redirect is
public function update_sticky_postition_in_db()
{
$this->autoRender = false;
... // Save info to database
$this->redirect(array('action' => 'tool'));
}
Where tool is the name of the view I am tring to redirect to. To try and speed the process of finding the problem I have checked few things and think I have found the cause of the problem. Basically I am trying to redirect to the view that is currently active which I think is part of the reason why it is not redirecting. I have read that it might have something to do with caching of the page but I am not sure how to solve that issue.
Also when using firebug I can see the redirect is sending a GET request but after that nothing is happening. Do I have to do something with the GET request or should Cake handle that for me. Also I have checked the URL of the GET and it is correct.
It is located within the controller with the correct name as I can view the original tool page.
Also the update_sticky_postition_in_db() method does not have a view (hence why the auto render is set to false), its intended purpose is to update a row in the database from an ajax call.
From your post it seems you're firing the update_sticky_postition_in_db() using ajax call, so that the redirection will not work.
You can do redirection using JavaScript within ajax success method.
In order to do that, you may send some json_encode() message from you above method and checking that status within ajax success method you can do a redirect using window.location.

Resources