Friday, 7 July 2017

How to create HMVC(Hierarchical Model View Controller)?

How to create HMVC(Hierarchical Model View Controller)?

Download HMVC Library from https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads/

It contains two folder folder core & third_party. Put files from those folder to your application/config/core & application/config/third_party

now check your site if it show error "Fatal error: Call to undefined method MY_Loader::_ci_object_to_array() in" then change Line 300 in third_party/MX/Loader.php
        
         return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
     with
        if (method_exists($this, '_ci_object_to_array'))
        {
                return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        } else {
                return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_prepare_view_vars($vars), '_ci_return' => $return));
        }
Now create Folder in application/config/modules/foo/views/yourview.php & application/config/modules/foo/controllers/Foo.php
create same way for another  application/config/modules/bar/views/yourview.php & application/config/modules/bar/controllers/Boo.php

now access
http://localhost/yourproject/index.php/foo
http://localhost/yourproject/index.php/bar

Now if you want to access bar controller in foo view you can call as

<?php
    // syntax Modules::run("module/controller/function");
    echo Modules::run("bar/bar/index");
   
?>


Codeigniter Route

$route['what-will-typed-in-url']='what-action-to-perform';

//ex. url will like http://localhost/ci_testing/index.php/user/123

for this kind of url we don't have any 123 function in user controller but if this kind of url comes so we have to call user/myprofile function which means myprofile function of user controller. so for this we will create a route as:

$route['user/:num']='user/myprofile/$1';



here :num indicates any number after user and $1 will pass that number to myprofile function

we have to define this route in routes.php in config folder

Monday, 6 June 2016

How to create Login Logout functionality with session ?

How to create Login Logout functionality with session ?



In Few Basic Steps we can create Login-Logout with session

Step 1 :   
   
    Load Libraries in autoload.php        //Location :    application/config/autoload.php
   
    $autoload['libraries'] = array('database', 'email', 'session');
   

Step 2 :   
   
    Load Drivers in autoload.php        //Location :    application/config/autoload.php
   
    $autoload['drivers'] = array('session');
   
   
Step 3 :   
   
    Load Helper Libraries in autoload.php        //Location :    application/config/autoload.php
   
    $autoload['helper'] = array('url', 'file');


Now Comes Logical part We will create Two Controller files & Two View Files One for Before Login & Another for After Login Functionalities

Our Two controller will "Account" and "User" and views will "login_view" and "dashboard_view"

Step 4 :   

    First Create view using simple form on login_view page
   
    Form Code:
   
    <form method="post" action="<?php echo site_url('account/login');?>">
        <table>
            <tr>
                <td><input type="text" name="email" value="" placeholder="E-mail"/></td>
            </tr>
            <tr>
                <td><input type="password" name="password" value="" placeholder="*****" /></td>
            </tr>
            <tr>
                <td><input type="submit" name="login" value="Login" /></td>
            </tr>
        </table>
    </form>


Step 5 :   
   
    Add login logic on Account Controller Page We will use simple & static you can use dynamic using database
   
    function login()
    {
        $email=$this->input->post('email');
        $password=$this->input->post('password');
        if($email=='test@gmail.com' && $password=='123456')
        {
            $session_data=array(
                                'id'=>1,
                                'email'=>$email,
                                'islogin'=>True,
                            );
            $this->session->set_data($session_data);
            redirect('user');
        }
    }
   
Step 6:

    On User Controller create the constructor function so that it will called before calling any function
   
    public function __contruct()
    {
        parent::__contruct();
        if((!$this->session->has_userdata('id'))||(!$this->session->has_userdata('email'))||(!$this->session->has_userdata('islogin')))
        {
            redirect('account/login');
        }
    }

Step 7:

    Create a function on User controller to show welcome page simply load welcome page
   
    function dashboard()
    {
        $this->load->view('dashboard');
    }
   
Step 8:
   
    Create dashboard view to show welcome message and user email id
   
    <a href="<?php echo site_url('account/logout');?>">Logout</a>
   
    <h3>Hello <?php echo $this->session->userdata('email');?><h3>
   
Step 9:

    Create Logout functionality on Account controller page
   
    function logout()
    {
        $this->session->sess_destroy();
        redirect('account/login');
    }

Sunday, 5 June 2016

Remove index.php from codeigniter url placed in subdomain



<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /mop/index.php/$1 [L,QSA]
</IfModule>


Here mop is folder name placed in public_html
public_html/mop/index.php


Note: Replace mop with your folder name

Remove index.php from url codeigniter

How to remove index.php from url codeigniter


Goto        :    application->config->config.php

Find         :    $config['index_page'] = 'index.php';

Replace        :    $config['index_page'] = '';

Now create new htaccess file & paste following code and save by name as .htaccess on root directory of your project


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


Now Refresh the url index.php is removed from url