Custom Login module creation in Magento

Dream. Dare. Do – that is Suyati’s work principle in a nutshell.

Sep
03
2013
  • Author:
  • NaveenBos

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.

 
magento contact button

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.

Comments (19)
Naveenbos (2 years ago)

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/

mukesh (2 years ago)

Hi, what is use of helloworld tag in config.xml and you also used in Mymodule.php file. Thank you

mukesh (2 years ago)

Hi, what is use of mymodule.xml in config.xml and you also used in Mymodule.php file. Thank you

Naveen (2 years ago)

Hi Mano, Thanks for the comments, we are planning for creating the registration module. Once again Thankyou.

Leave a Comment

Your email address will not be published. Required fields are marked *