Redirecting From Logout Page Directly to Current Page
When it comes to managing user sessions and logouts in Magento, you might find yourself wanting to streamline the process by redirecting directly from the logout action to the current page rather than displaying a separate logout confirmation page first.
The default Magento setup includes a dedicated logout page (customer/logout.phtml) that displays a brief message before redirecting back to the homepage after five seconds. You’ve correctly identified where this redirection occurs in both your template and customer.xml configuration file.
Understanding the Default Behavior
In the logout.phtml template, you’ll notice the following:
<div class="page-head">
<h3><?php echo $this->__("You're now Logged Out") ?></h3>
</div>
<p><?php echo $this->__('You have been successfully logged out and will be redirected to our homepage in 5 seconds.') ?></p>
<script type="text/javascript">
setTimeout(function(){ location.href = '<?php echo $this->getUrl() ?>'},5000);
</script>
This script displays a message, waits for five seconds, then redirects the user back to the root URL of your store using JavaScript.
Redirecting Directly After Logout
To achieve the desired behavior of redirecting directly to the current page after logout, you’ll need to modify both the logout action in your controller and the template file. Here’s how:
Step 1: Update the Logout Controller
Open app/code/core/Mage/Customer/controllers/AccountController.php. Find the logoutAction() method within this class.
public function logoutAction()
{
$session = Mage::getSingleton('customer/session', array('name' => 'frontend'));
$session->logout()->redirect('*/*/');
}
In the above code, the redirect() method is called with '/*/' as an argument. This means that after logging out, Magento will redirect to the current path or route.
Step 2: Remove the Logout Template
Since you’re now handling the redirection in the controller, you can remove the template file completely for cleaner code and improved performance.
Navigate to app/design/frontend/base/default/template/customer/ and delete the logout.phtml file. This will prevent Magento from rendering a separate page after logout.
Step 3: Test Your Changes
Clear your cache and test the logout functionality on your site. Once logged in, click the “Logout” link. You should be immediately redirected back to the current page without seeing any intermediate messages or pages.
Conclusion
By modifying the logout action in the controller and removing the unnecessary template file, you’ve successfully achieved a smoother user experience by redirecting directly from the logout process to the current page in Magento.
This approach not only improves the speed of your site but also simplifies the codebase. It’s a great example of how understanding and customizing core Magento functionality can lead to more efficient results.
Remember, always make a backup before editing core files, as this change is considered an override that will be overwritten during an upgrade unless you use a third-party extension or custom theme for your modifications.
For further guidance on customizing Magento logic and templates, feel free to explore the Laravel Company blog. We regularly publish articles on advanced Magento development techniques.