How to create your own custom module or extension in magento

Magento

Posted on 02 Jun 15 by Vickey
  821

From magento community we can install many extension or module.These module provided by magento connect and from third party developers. By these modules or extension we can solve many difficult task without programming.But if we are working on specific task which is not available on magento community then we need to create our own module with own logic. but for creating own custom module there is need good programming skill and good knowledge of magento structure.

All types of modules located in "/app/code" folder here are three types of module 1.Core: Here are the core functionalites stored which come inbuilt with magento installation 2.Community: These comes from third party developers which are paid and free 3.Local: Here we create our custom module

Lets strat ton create custom module

First go to directory " /app/code/" and create a directory name as "local" if not exist now open local directory and here we will create a new directory which will be the name space of our module for example i am creating it with my name "Vickey" don't use underscore or space in this name. now open namespace directory and create a subdirectory and give a name which will be the module name i am giving name "Hello". now we have namespace "Vickey" and module name "Hello".For use this module property we need to declear the module.

go to "/app/etc/modules" and create a xml file with the name of namespace and module name for example Vickey_Hello.xml

and add the code like this

<?xml version="1.0"?>
<config>
         <modules>
                <Vickey_Hello>
                        <active>true</active>
                        <codePool>local</codePool>
                </Vickey_Hello>
         </modules>
</config>

Now go to directory "/app/code/local/Vickey/Hello" and create a directory "controllers" open this and create a file "IndexController.php" and put the below code in this file

<?php
class Vickey_Hello_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
     $this->loadLayout();
     $this->renderLayout();
    }
}
?>

after creating controller again go to "/app/code/local/Vickey/Hello" create directory Block and create a file "Hello.php" and put code

<?php
class Vickey_Hello_Block_Hello extends Mage_Core_Block_Template
{
  methods
}

?>
create a file "/app/code/local/Vickey/Hello/etc/config.xml" and put code
<?xml version="1.0"?>
<config>
    <global>
        <modules>
                <vickey_hello>
                        <version>0.1.0</version>
                </vickey_hello>
        </modules>

    <blocks>
            <hello>
                <rewrite>
         <hello>Vickey_Hello_Block_Hello</hello>
        </rewrite>
            </hello>
     </blocks>

        </global>
       <frontend>
                <routers>
                        <hello>
                                <use>standard</use>
                                <args>
                                      <module>Vickey_Hello</module>
                                      <frontName>hello</frontName>
                                </args>
                        </hello>
                </routers>
        <layout>
            <updates>
                <hello>
                      <file>hello.xml</file>
                </hello>
            </updates>
            </layout>
        </frontend>
</config>

Now go to your theme which is set for your magento frontend

"/opt/lampp/htdocs/magento/app/design/frontend/my_theme/default/layout" and create a file "hello.xml" and add code

<?xml version="1.0"?>
    <layout version="0.1.0">
        <hello_index_index>
            <reference name="root">
                <action method="setTemplate"><template>page/1column.phtml</template></action>
            </reference>
            <reference name="content">
                <block type="hello/hello" name="hello" template="hello/hello.phtml"/>
            </reference>
        </hello_index_index>
    </layout>

after completing saving this file go to directory "/app/design/frontend/my_theme/default/template" and create a subdirectory "hello" with a file name as hello.phtml and put something which you want to show on webpage and you have done now open your browser put url "your domain name/index.php/hello"

I hope this will help you to creating your first custom module

How to get all category of a magento store

Magento

Posted on 02 Jun 15 by Vickey
  621

By using below code we can get all category which is come on level 2 if we remove level filter then it will return all parent category and base category

$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('id')
->addAttributeToSelect('name')
->addAttributeToSelect('urlkey')
->addAttributeToSelect('url')
->addAttributeToFilter('level',2)
->addAttributeToSelect('isactive');

foreach ($categories as $category)
{
        $name = $category->getName();
        $url_path = $category->getUrl();

}

if you want to get child categories of a category then you can use this code

$children = Mage::getModel('catalog/category')->getCategories(10);

here 10 is the parent category id

How to get all existing values of a specific attribute in magento

Magento

Posted on 02 Jun 15 by Vickey
  515

In a project i had a custom attribute name as "company" and i need to add a filter on my search page so that i can filter my products by company name so i fetch all existing values used for "company" attribute by using these value i created a dropdown to show all values

here is example

$attributename= 'company';
$get_product_attributes = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToFilter($attributename, array('notnull' => true))
            ->addAttributeToFilter($attributename, array('neq' => ''))
            ->addAttributeToSelect($attributename);
$companyList = array_unique($get_product_attributes->getColumnValues($attributename));

in $companyList variable i got all values saved as company name

How to create custom magento theme

Magento

Posted on 30 May 15 by Vickey
  495

If you are new in magento then this blog for you.after install magento you need to change your front end design or theme according to your requirement so that you can give new look to the website.In this blog first we understand the structure of Magento theme. when you go in "app/design/frontend" you will see some default themes directory for example

app/design/frontend/default

app/design/frontend/base

these are the default themes in magento. Now go to "/skin/frontend" directory here you will see the same directory as you see in "app/design/frontend". In "/skin/frontend" directory default css and images are stored

so lets start to create new custom theme

Step 1: got to directory "app/design/frontend" and create a copy of "base" directory and rename it which name you want to give your new theme here i am renaming it to newtheme

Step 2: now open directory "/skin/frontend" and copy "default" directory and rename it with the name of your new theme now you have create your new theme

Step 3: Now login your admin pannel and open system >> Design here you will see a button "Add Design Change" click on it and you will see a form name a "General Settings" select your theme under theme package for example as i am using my custom theme "newtheme" then i will select newtheme >> defualt and save this.now my new theme has been set. But this will look like defualt theme so we need to edit this

Step 4: First go directory "/js" which is located at your magento root folder and put your all js files here.

Step 5: Got to directory "/skin/frontend/newtheme/default" and put your css and images here

Step 6: Now go to "/app/design/frontend/newtheme/default/layout/ and open file page.xml and search "page/html_head" you will see code like this

<block type="page/html_head" name="head" as="head">
<action method="addJs"><script>js/jquery.js</script></action>
<action method="addJs"><script>lib/somejs.js</script></action>

here add your theme js file like this

<action method="addJs"><script>my_new_js_file.js</script></action>

amd add your css file for new theme

<action method="addCss"><stylesheet>css/your_styles.css</stylesheet></action>

you have set your css and js files and now we will work our html part 

Step 7: Go to directory /app/design/frontend/newtheme/default/template/page/html/ and open file "header.phtml" comment old code and put your header ocde here and save

Step 8: Open "footer.phtml" and do same with footer

Step 9: Now open "1column.phtml" and make changes in html according to your design do same for "2columns-left.phtml" , "2columns-right.phtml", "3columns.phtml" if your using these for any page you can set these layout from admin pannel. for change layout go to CMS >> Pages and change your layout for different pages and now open your. when you will refresh your page you will see changes

How to use form validation in laravel

Laravel

Posted on 22 May 15 by Vickey
  545

We have inbuilt validation in laravel by using the laravel validation we can validate our form very easily In this tutorial i have a form which have name and email and password fields for validate this form i use the below code

$userdata = array(

  'name' => Input::get('name'),
  'email' => Input::get('email'),
 'password' => Input::get('password'),

);

$rules = array(
    'name'=>'required|alpha_num|min:5',
       'email'=>'required|unique:users,email|email',
      'password'=>'required|alpha_num|min:5'
);

$validator = Validator::make($userdata, $rules);

if($validator->fails())
{ 
    return Redirect::back()->withInput()->withErrors($validator);
}
else{
              // save record in databse

}

for show validation message on view use

     @if($errors->any())

        {{ implode ('', $errors->all('<p><strong>:message</strong></p>'))}}

    @endif

Cache Database Queries in laravel

Laravel

Posted on 22 May 15 by Vickey
  435

if we have a common query which is fire on each page for example a i want to show sidebar to show subject list so i need same data on each page when i use query on each page it interact with database many time when we change our page.But in laravel we can cache our queries. by doing this no need to interact with database many times and the result of this the speed of our web application will increase.

$questions = Question::remember(60)->get();

this code create query cache for next one hour and there is no need to interact with database for next one hour.

How to use Model binding in larvel

Laravel

Posted on 21 May 15 by Vickey
  497

I have two model Blog and User. One user can write many blogs for bind both model we can use the below code

class Blog extends Eloquent {

    public function user()
    {
        return $this->belongsTo('User');
    }

}



class User extends Eloquent {

    public function blogs()
    {
        return $this->hasMany('Blog');
    }
}


$user = User::find(1);
$blogs = $user->blogs;


$blogs = Blog::find(1);
$user = $blogs->user;

How to render a view without controller in laravel

Laravel

Posted on 21 May 15 by Vickey
  1008

Render a view without controller in laravel

Route::get('home', function()
{
    return View::make('home.index')->with('subjects', subject::all());
});

you can use this in a small projects but if your projects is big then you need to create controller

Database engines in mysql

Mysql

Posted on 21 May 15 by Vickey
  577

Here is the list of database engines supported by mysql

MyISAM

InnoDB

Memory

CSV

Merge

Archive

Federated

Blackhole

InnoDB is the defualt storage engine in version 5.5 before version 5.5 MyISAM was default engine

MyISAM: The table size limit of Myisam engine is 256TB. On startup MySQL inspections MyISAM tables pertaining to corruption as well as mend all of them in the event of mistakes. MyIsam engine does not support transactions.mostly this is used in Web, data warehousing.

InnoDB: This engine support transactions and it support crash recuperation in addition to multi-version concurrency control. This engine engine provides foreign key referential integrity constraint. The table size limit of InnoDB engine is 64TB.

Memory: This engine also known as heap. This engine create tables on memory and the lifetime of tables depends on server and when server restarted data lost because it creates temporary tables for quick lookups

CSV: In this engine data store in a csv file. This is very flexible in this format data is easily integrated with other applications.This engine does not support null data type

Merge: This engine combine multiple tables which have same structure. This is known as MRG_MyISAM.By using merge table we can increase speed by joining multiple tables. It allow only select update insert delete operations on merge table. if you use drop table then this will remove only merge specification.

Archive: This can store large number of record.when a record is inserted in it compress it and on reading decompress by using zlib library.This engine only allow insert and delete operation.

Federated: This engine manage data from a remote server and no data store in locally.When we use query on local server it automatically execute on remote server.

Blackhole: This engine accept data but not store.this used for testing purpose.

How to use Blowfish password hasher in cakephp

Cakephp

Posted on 14 May 15 by Vickey
  1482

Blowfish password hasher is very strong. it is very difficult to hack. you can use blowfish hasher by following these steps.

In your model add this code

public function beforeSave($options = array()) {

        if (isset($this->data[$this->alias]['password'])) {
            $passwordHasher = new BlowfishPasswordHasher();
            $this->data[$this->alias]['password'] = $passwordHasher->hash(
                $this->data[$this->alias]['password']
            );
        }

        return parent::beforeSave($options);
    }

it will hash your password before save record

and for use blowfish hash password add this in auth component

public $components = array('Session', 'Cookie',
    'Auth' => array(

                'authenticate' => array(
                    'Form' => array(
                            'passwordHasher' => 'Blowfish',
                            'userModel' => 'User',
                            'fields' => array(
                            'username' => 'username',
                            'password' => 'password'
                        )
                    )
                )

    ),
);
www.000webhost.com