Magento is an open source ecommerce solution. Magento module development have a certain standard procedures. Let us look in to the new custom module creation.
Create one folder local inside app/code/. This local folder is used for our custom module creation. This folder will not affect any upgrade of magento in future if any.
/app/code |- /core/ |- /community/ |- /local/
Inside this local folder create another folder called as Name Space(Test), inside that folder create another folder, it is usually called as modulename(Mymodule).
App/code/local/Test/Mymodule.
app/code/local/Test/Mymodule/
here Test is our Name Space and Mymodule is our modulename.
How to activating the module
Create a file Test_Mymodule.xml in app/etc/modules/, this will tell Magento to look for and use our custom module.
How to activating the module
Create a file Test_Mymodule.xml in app/etc/modules/, this will tell Magento to look for and use our custom module.
true local
create config.xml file in app/code/local/Test/Mymodule/etc
0.1.0 standard Test_Mymodule mymodule mymodule.xml Then create a controller file called as IndexController.php in app/code/local/Test/Mymodule/controllers
inside that file write our indexaction
public function indexAction() { $this->loadLayout(); $this->renderLayout(); }
this will load the layout and render the view file
Create login Action function
public function loginAction() { $session = Mage::getSingleton('customer/session'); if ($session->isLoggedIn()) { // is already login redirect to account page return; } $result = array('success' => false); if ($this->getRequest()->isPost()) { $login_data = $this->getRequest()->getPost('login'); if (empty($login_data['username']) || empty($login_data['password'])) { $result['error'] = Mage::helper('onepagecheckout')->__('Login and password are required.'); } else { try { $session->login($login_data['username'], $login_data['password']); $result['success'] = true; $result['redirect'] = Mage::getUrl('*/*/index'); } catch (Mage_Core_Exception $e) { switch ($e->getCode()) { case Mage_Customer_Model_Customer::EXCEPTION_EMAIL_NOT_CONFIRMED: $message = Mage::helper('onepagecheckout')->__('Email is not confirmed. Resend confirmation email.', Mage::helper('customer')->getEmailConfirmationUrl($login_data['username'])); break; default: $message = $e->getMessage(); } $result['error'] = $message; $session->setUsername($login_data['username']); } } } $this->_redirect('customer/account/'); //$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); }
Now let’s create mymodule.xml file in app/design/frontend/default/default/layout/
Create a template file callled as view.phtml in app/design/frontend/default/default/template/mymodule
this is our login form
create a Block file called as Mymodule.php in app/code/local/Test/Mymodule/Block/ the block folder might not be there, we have to create the same.
type this url in your browser and check the custom login module http://example.com/mymodule where example.com will be your domain name.
19 comments
Dear sir
pliz help me:
I have a step by step add this code but not show a login form in my website so pliz help me..
From-shailesh patel
Hi Shailesh patel,
Thankyou for contacting us.
Please logout and login in the admin panel and navigate System -> Configuration -> Advanced -> Disable Modules Output scroll down to see whther Test_Mymodule is “enabled” , if it is not there, check your app/etc/modules/namespace_modulename.xml file for any mistakes.
Please clear your cache or you can disable cache by going to admin panel -> System -> Cache Management -> Select All -> Action:Disable -> Submit.Otherwise it will distrub you during the development process.
Please make sure that Web Server Rewrites is Enabled, for that Navigate to admin panel -> System -> Configuration -> General -> Web -> Search Engines Optimization tab -> Use Web Server Rewrites -> Action: Yes -> Save Config.
Please logout and login in the admin panel and navigate System -> Configuration -> Advanced -> Disable Modules Output here we can see our module, if it is not, check your app/etc/modules/namespace_modulename.xml file have any mistakes
Please give url like this http://example.com/mymodule
or http://example.com/index.php/mymodule.
Hi,
Thanks for the post.
I am able to view the login form using this module. But its redirection is not working after button click.
Please help me the redirection.
Thanks in advance!
Hi Darshana Sangwan,
Thankyou for contacting us.
Make sure that your redirect url is enabled.
Please logout and login in the admin panel and navigate System -> Configuration -> General -> Web -> Search Engine Optimization -> Use Web Server Rewrites -> Yes, then only it will work perfectly.
Hi Naveen,
Thanks a lot for reply!
I have tried all these options but its not working. Please if possible can you provide a zip file for the above all code.
Thanks in advance!
I have checked here, it is working fine for me. please disable your cache and flush Magento cache, cache storage,catalog image cache, and javascript/css cache, you can write your own redirect script, here we are using $this->_redirect(‘customer/account/’); for redirecting.
Hello! Thank you for this tutorial, it’s definitely helping me get started. I have a question though – how can I get magento to use this module for all logins? as i understand it right now, one must go to the “example.com/mymodule” link to make use of this – is there a way to force the login links on the website to go to this link? Thank you!
How does the helloworld tag in the config.xml relate to anything for authenticating users logging in?
Hi Charles,
We have to change the magento default login urls. Then we will get our custom login module.
Hi Ken,
Helloworld tag is not related to the user login, user login action written in the indexcontroller file. Here i have used the helloworld tag for rendering the mymodule.xml file. Through the mymodule.xml file i am loading the template called view.phtml , view.phtml file contains our login form. we can use any thing instead of helloworld. but that tag need to mention in the blok class name other wise it won’t load the templates.
I did all but cant see the login form, new in Magento, my “mymodule” url is rendering in my localhost but cant see the login box.
is there any thing that i have missed ?
Hi Rahul Goswami, Thankyou for your reply, i am not sure about that, if you missed any steps you wont get the login form.You have to follow the each and every steps that i have mentioned in the blog. i am sure that you will get the login form. Please remove the existing one and redo the all steps.
Hi,
How would you create a custom page for failed authentication ?
I thought that it’s better to have all your logic in blocks ?
how would you pass from this block that doesn’t have any logic to view ?
Hi Marko,
Thanks for your query, we can show the failed authentication in the same page. for that there is a slight modifiction in our view.phtml form. please replace the existing view.phtml with the below code.
http://pastebin.com/F45CB1Sp
For more clarification about extension creation in magento please visit http://www.magentocommerce.com/magento-connect/create_your_extension/
Hi Thanks for providing such a good Tutorial. I need to check with the custom registration table data for login functionality with magento. It is helpful for me if you provide similar tutorial or reference link.
Thank you in advance.
Hi Mano,
Thanks for the comments, we are planning for creating the registration module. Once again Thankyou.
Hi,
what is use of
mymodule.xml
in config.xml and you also used in Mymodule.php file.
Thank you
Hi,
what is use of helloworld tag in config.xml and you also used in Mymodule.php file.
Thank you
Hi Mukesh,
Thanks for your query, Here i have used the helloworld tag for rendering the mymodule.xml file. Through the mymodule.xml file i am loading the template called view.phtml , view.phtml file contains our login form. we can use any thing instead of helloworld. but that tag need to mention in the blok class name other wise it won’t load the templates.
In config.xml file i am rendering the mymodule.xml file using the helloeworld tag, as per the magento plugin structure.
http://www.magentocommerce.com/magento-connect/create_your_extension/