Create Simple Web Installer For Codeigniter Application

October 24th, 2012 by aditia rahman / No Comments  

     

When you installing a fresh cms on your server usually you will find a form for setting up your database setting, admin, etc, this feature often can be found on popular cms. In this post I try to create simple installer for codeigniter application, this might help after you finish create your own custom web project and don’t want to make confuse your client with manual install by editing php file.

This script will change some of default codeigniter setting they are

- index.php

- application/config/config.php

- application/config/database.php

Just make sure they are writable (I’m using windows, no permission should be changed)

1. Create MY_Install library (MY_Install.php)

Create this file on codeigniter application library, I assume we have a default codeigniter database setting, so this script on check if database name are blank we can continue on installation, by redirecting the page to install folder.

<?php

class MY_Install {
    
    public function __construct() {
        $CI =& get_instance();
        $CI->load->database();
        if ($CI->db->database == "") {
            header('location:install/');
        } else {
            if (is_dir('install')) {
                echo '<i>Plese delete or rename <b>Install</b> folder</i>';
                exit;
            }
        }
    }
    
}

2. Include the library on autoload.php

$autoload['libraries'] = array('database', 'MY_Install');

3. Create install folder

install folder

After install folder created, create index file consist of form script for database setting and php script for replacing the default codeigniter settings.

Form script

<?php
$error = 0;
if (isset($_POST['btn-install'])) {
    
    // validation
    if ($_POST['inputDBhost'] == '' || $_POST['inputDBusername'] == '' || 
            $_POST['inputDBname'] == '' ||
            $_POST['inputSiteurl'] == '' || $_POST['inputAppfolder'] == '' || 
            $_POST['inputSystemfolder'] == '' || 
            ($_POST['inputAppfolder'] == $_POST['inputSystemfolder'])) {
        $error = 1;
    } else {
        
        @$con = mysql_connect($_POST['inputDBhost'], $_POST['inputDBusername'], $_POST['inputDBpassword']);
        @$db_selected = mysql_select_db($_POST['inputDBname'], $con);

        if (!$con) {
            $error = 1;
        } else if (!$db_selected) {  
            $error = 1;
        } else {
            // setting site url
            $file_config = "../application/config/config.php";
            $content_config = file_get_contents($file_config);
            $content_config .= "\n\$config['base_url'] = '".$_POST['inputSiteurl']."';";
            file_put_contents($file_config, $content_config);

            // setting database
            $file_db = "../application/config/database.php";
            $content_db = file_get_contents($file_db);
            $content_db .= "\n\$db['default']['hostname'] = '".$_POST['inputDBhost']."';";
            $content_db .= "\n\$db['default']['username'] = '".$_POST['inputDBusername']."';";
            $content_db .= "\n\$db['default']['password'] = '".$_POST['inputDBpassword']."';";
            $content_db .= "\n\$db['default']['database'] = '".$_POST['inputDBname']."';";
            file_put_contents($file_db, $content_db);

            // setting folder name
            $file_index = "../index.php";
            $content_index = str_replace("\$system_path = 'system';", "\$system_path = '".$_POST['inputSystemfolder']."';", file_get_contents($file_index));
            file_put_contents($file_index, $content_index);
            $content_index = str_replace("\$application_folder = 'application';", "\$application_folder = '".$_POST['inputAppfolder']."';", file_get_contents($file_index));
            file_put_contents($file_index, $content_index);

            // rename app folder
            $index = str_replace('install', '', dirname(__FILE__));
            rename($index.'application', $index.$_POST['inputAppfolder']);
            rename($index.'system',      $index.$_POST['inputSystemfolder']);

            header('location:../');
        }
    }
}

?>

PHP Script

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="bootstrap.css"/>
</head>
<body>
<div class="container">
    <div class="row-fluid">
    <h4>Install Your Codeigniter App</h4>
    <form class="form-horizontal" action="" method="post" style="margin-top:30px;">

        <?php if ($error == 1): ?>
        <div class="alert alert-error" style="font-size:11px;">               
            <b>Opps error ... </b> please check: 
            <br/> - <i>Each fields cannot be blank</i>
            <br/> - <i>App Folder and System Folder cannot the same</i>
            <br/> - <i>Database name must exist on your MySQL</i>
        </div>
        <?php endif; ?>
        <legend>Database Settings</legend>
        <div class="control-group">
            <label class="control-label">DB Host</label>
            <div class="controls">
                <input type="text" name="inputDBhost">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Name</label>
            <div class="controls">
                <input type="text" name="inputDBname">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Username</label>
            <div class="controls">
                <input type="text" name="inputDBusername">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">DB Password</label>
            <div class="controls">
                <input type="password" name="inputDBpassword">
            </div>
        </div>

        <legend>App Settings</legend>
        <div class="control-group">
            <label class="control-label">Site URL</label>
            <div class="controls">
                <input type="text" name="inputSiteurl">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">App Folder</label>
            <div class="controls">
                <input type="text" name="inputAppfolder">
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">System Folder</label>
            <div class="controls">
                <input type="text" name="inputSystemfolder">
            </div>
        </div>
        <div class="control-group">
            <div class="controls">
                <input type="reset" class="btn" name="btn" value="Reset"/>
                <input type="submit" class="btn btn-primary" name="btn-install" value="Install"/>
            </div>
        </div>
    </form>
    </div>
</div>
</body>
</html>

That’s all after the installation finish it will redirect to index page again and ask you to rename or remove install folder, you can add more validation by yourself or even other settings for common web application, below are the example screenshot.
ci installer

        submit to reddit Delicious

No Comment Leave a Comment Subscribe RSS

Leave a Comment