How to Make Most Out of CodeIgniter

Welcome to the updated official user guide of CodeIgniter Wizard.

Introduction

Welcome to CodeIgniter Wizard for Mac.

CodeIgniter Wizard is a PHP developer utility that helps quickly generate an advanced starting point for MySQL database-driven web applications with administrative interfaces by generating the most commonly used boilerplate code for CRUD operations and HTML files.

With its database-first approach, the wizard asks you to select the tables and fields you want to include in your application, and then it automatically generates the necessary models, views, and controllers - even the new CodeIgniter 4 entities, supporting standard database CRUD operations, that is, create, read, update and delete. It even populates sidebar and (dashboard) home pages automatically with some basic data from your modules.

A design-first approach is also planned for an upcoming release in the future.

CodeIgniter Wizard generates most of the time-consuming boilerplate code needed for the basis of the backend of most web applications which will save you weeks if not months! You can then take the generated project and keep developing using the base code.

Most important of all, you are not bound to any CodeIgniter Wizard-specific libraries or base classes to further develop your application. Every code file CodeIgniter Wizard generates is standards-based, generic, secure, re-useable, yet simply replaceable.

What's New in CodeIgniter Wizard

  • As of version 1.3.8, the module generator now automatically detects if there were changes made in MySQL after the module was generated, and it adjusts the current state of the fields to adapt to the new structure.
  • The ability to specify sub-levels to namespaces of MVC classes has also been added as of v.1.3.8.
  • As of version 1.4, in addition to custom namespaces for MVC classes, now there's also the ability to specify subfolders of any depth in the hierarchy for view files.
  • "Drag and drop" sorting of modules and created fields / columns in table views. Based on this specified order, fields in forms, and columns in grid-style tables are laid out accordingly in the generated HTML.
  • The ability to filter modules and fields in table views using a search / filter box in CodeIgniter Wizard UI.

CodeIgniter Wizard Requirements

You can download CodeIgniter Wizard from the Mac App Store. It currently runs on Macs with macOS High Sierra (10.13) and later.

In order to be able to make CodeIgniter Wizard serve its purpose, you separately need to have a MySQL or MariaDB server up and running.

The web applications created with CodeIgniter Wizard are PHP code using version 4.x of the the open-source CodeIgniter framework. Although CI Wizard does not require a PHP environment to generate code files forming a web app, the code generated with it does.

Requirements for CodeIgniter Wizard

  • macOS 10.13 or later
  • Minimum screen resolution of 1440x900
  • MySQL / MariaDB 5.5 or later


Requirements for Generated PHP applications

  • PHP 7.2 for CodeIgniter 4.0.x - PHP 7.3 or later for CodeIgniter 4.1.x, with *intl* extension and *mbstring* extension installed.
  • The following PHP extensions should also be enabled on your server: php-json, php-mysqlnd, php-xml

  • Access to the database server as indicated in the '.env' file or app/Config/Database. (You can specify a different database than MySQL / MariaDB via custom code outside CI Wizard.)
    The database platforms below are supported by CodeIgniter 4.x.
    • MySQL (5.1+) via the MySQLi driver
    • PostgreSQL via the Postgre driver
    • SQLite3 via the SQLite3 driver
    • MSSQL via the SQLSRV driver (version 2005 and above only)

CodeIgniter Wizard will run on macOS 10.13 or later (it is also macOS Big Sur 11.x-savvy), and it currently supports only MySQL / MariaDB for the database platform at design time. So you need to have access to a MySQL or MariaDB Server v.5.5 or later over either your local network or the Internet (SSH tunneling option not available).
Ideally, a local MySQL would be present on your Mac, however, it is not required. You can use any MySQL/MariaDB server on your network and user account to which you have direct access (SSH-tunneling-only access is not supported by CodeIgniter Wizard, though).

CodeIgniter Wizard currently does not create, modify or delete schemas, tables, and users on your database servers which you have to do by yourself either using a free GUI tool like MySQL Workbench, Sequel Ace (aka Sequel Pro), or a premium tool such as Navicat or simply by using the command line.

Current Known Limitations

Single Database

While it is possbile to configure the CodeIgniter framework itself to use multiple databases / schemas, CodeIgniter Wizard currently supports one database schema per web application. If you need to develop and deploy a web application using multiple databases and some of the entity models using one database and the others another, this is possible by manually customizing a web application previously generated with the wizard. (In fact this is how the CMS of the Ozar.net website currently works). More information on such a configuration is in the CodeIgiter 4 documentation.

Authentication and Authorization

The current version of CodeIgniter Wizard cannot automatically add user authentication and authorization for the modules you generate using it. That said you can add these by yourself on top of the code generated by CodeIgniter Wizard with custom development. Adding user authentication / authorization to webapps created with CodeIgniter Wizard can be done by writing your own code and/or integrating an existing user authentication and authorization framework such as "Myth Auth" using your own IDE on top of the generated code base by the Wizard.

CodeIgniter Wizard Application Settings / Preferences

Settings that will be applied to all of the web applications and code you will be creating, are configured in CodeIgniter Wizard Preferences accessible from the application menubar.

CodeIgniter Wizard 1.4 Preferences

PHP Settings

The timezone along with the date and time formats you want to apply to the generated PHP apps are set here.

Please note that if you make changes to any of these settings, they will not be automatically applied to the previously generated code files. If you both change settings and want them applied to the previously generated code, you must edit the existing module(s) and click "Generate Code and Close" (even if you do not change anything else in the module editor) and then re-publish that module.

Most of the other check-boxes below the 'time format' field are fairly self-explanatory except:

  • Publishing your CodeIgniter4 web applications directly in a shared htdocs (or www) directory of a web server such as MAMP or XAMPP is a bad practice and a security flaw.

    In fact, you should always create a virtual host pointing to the public directory of your web application, and set your URL accordingly such as:
    http://yourapplication

    However, if you insist on not using a virtual host, your routes and some of the URL links in your web application may not work properly even if you have set your application URL to
    http://localhost/yourapplication/public
    In such cases, make sure you checked Always prepend 'base_url()' to form actions and redirect URIs.

  • Use 'type="module" for inline and 'defer' for externally loaded JavaScripts for your externally referenced JavaScript files and inline JavaScript code to benefit from the "deferred" loading feature of modern browsers to reduce content render-blocking. In other words, your web pages will be loaded faster with this option - however, it will require you to write all of your custom inline JavaScript with <script type="module"> instead of <script type="text/JavaScript">.. If you add custom external JavaScript files later that interact with any of the dynamically created or edited DOM elements, you will also need to load these with the defer attribute.

Module Generator Preferences

When creating or editing a module, you can re-order fields to your liking to change the order of appearance in the generated list and/or form views.

Dragging and dropping for re-ordering a field (read from the database server) re-orders:
  • Position in Grid: You want this option if you would like to change only the order of columns in the HTML of data tables to be generated.
  • Position in Form: Select this option if you would like to change the order of fields only in the HTML of forms to be generated.
  • Both: This is the default option which, when selected, the order of both the form fields and data table columns is changed.
Name and Website Link for Copyright Notice in Footer

The text you will enter here will be published in the footer of the web applications you create with the wizard.

Defining a MySQL / MariaDB database Connection

Define a database once, and use it throughout all applications you create.

Databases Introduction

A database record is the first building block in CodeIgniter Wizard as all the applications you are going to create will need a database.

CodeIgniter Wizard generates your application's models, controllers, and views based on the data it reads from the database you register.

Currently only MySQL and MariaDB are supported.

CodeIgniter Wizard has been tested with MySQL 5.7 and MariaDB 10. The compatibility of web applications generated with CodeIgniter Wizard is the same as the CodeIgniter framework's own compatibility range.

Installing and Setting Up a Database

CodeIgniter Wizard does not install or set up database servers, nor does it create, modify or delete schemas, tables, and DB users on your database servers. You need a MySQL / MariaDB server up and running beforehand, and have access to it with necessary privileges on a given schema. You can then use a free GUI tool like MySQL Workbench, Sequel Ace (aka Sequel Pro), or a premium tool such as Navicat, or simply by using the command line (Terminal.app) to create your tables.

Your database server can either be installed on your local computer, or a computer on your network, or even on a hosting server (or cloud) accessible on the Internet.

Defining a Database Connection in CodeIgniter Wizard

Databases toolbar icon in CodeIgniter Wizard

In the application's main window, click the Databases toolbar icon. This will open up a new window titled 'Database Records'.

Database Records Screenshot in CodeIgniter Wizard

The left part of the 'Database Records' window is where you can see a list of databases you registered, and the right-hand side is where you can register a new database in CodeIgniter Wizard.

In the custom name field, you can give your "database record" any name which you will recognize later, such as "Customers DB on MAMP". All the information you enter in this form will be stored in the local database of CodeIgniter Wizard, except the database user password which will be stored in the standard KeyChain of macOS.

After entering your credentials, and verifying your connection, remember to save them by clicking 'Save'.

Creating a Web Application in CodeIgniter Wizard

Creating a New Project

When you launch CodeIgniter Wizard, the main window will default to the new application form.

If you clicked any previously created applications in the left-hand side pane, you would have been taken to a slightly different view presenting the current state of that application.

If that's the case, you can always start creating a new application by clicking the "+" button at the bottom-left corner.

Screenshot of 'Creating a new web application' in CodeIgniter Wizard

Fill out the basic information for your application on this form, such as the application's name, the database it uses. Please note that once you click 'Save...' at the bottom of the form, the database you have chosen here cannot be changed later for this very application, so decide carefully. (If you need to use a different database later, you will then need to create a new web application.)

Basic Information (previously labelled 'Required Information')

These are the basic settings to generate a new blank application. Below is the detailed breakdown of the fields you need to fill in.

Application name:
Enter a human-friendly name for your application and keep it as short as possible. What you enter here will appear in the "brand" section of the generated views.
Database:
Select a database from the dropdown list. The database(s) you have previously "registered" will pop up in this menu.
Coding Language:
Since CodeIgniter is a PHP framework, it will be selected by default.
Backend framework:
Currently, only CodeIgniter 4.x is supported. If you would like to have CodeIgniter 3.x there as an option, you can submit a feature request and/or sign a petition for us to consider adding this.
Front-end CSS framework:
This is the CSS-based template for the view layer of your application. Select the CSS framework and/or theme of your choice here. Currently, only Bootstrap 4 and Bootstrap-based Admin LTE themes are supported.
Auto-generate layout files for views:
When checked, your application's view files will use layout template format (which is similar to Laravel's blade template files except for the mustache-like double curly brace syntax ). If unchecked, the generated view files will just include simple, old-school header and footer PHP files to retain the template look and feel.
Location (previously labelled 'Additional Info')

This is the section where you need to specify the path and folder to your application in the Finder and the URL.

Prior to version 1.5 of CodeIgniter Wizard, the folder selection here was restricted to your user's Downloads folder in order to avoid some quirks arising from new file/folder permission policies of macOS because of increased security. As of version 1.5 CodeIgniter will allow to create your project outside your Downloads folder.

Once your selection is complete, the last folder name in the path will show up in the next field labelled 'Application Folder Name' so you will know the exact full path set. .

Application folder path:

Click the ellipsis (...) button next to the corresponding field. This will bring up macOS' default file & folder selector dialog box, where you can specify the path to your application. The path must be inclusive of your application's own folder, so in most cases, you will typically need to create a new folder inside the folder selector dialog panel, and carefully name it as the folder of your application.

Once your selection is complete, the last folder name in the path will show up in the next field labeled 'Application Folder Name' so you will know the exact full path that was set.

You can change this by clicking the ellipsis button again before you 'Save...'.

Application folder name:
You cannot change the value in this field directly as it is set using the ellipsis (...) button as described right above. (You can select by highlighting, and copy the folder name though)
Coding Language:
Since CodeIgniter is a PHP framework, PHP will be selected by default.
Application URL:
This is the base URL of your web application. It is 'http://localhost:8080' by default. If you run the command 'php spark serve' in the root directory of your web app folder in a command-line Terminal, your app will immediately be accessible using PHP's built-in web server, via this URL.

You can change it to whatever your web server is configured for if you are using a web server such as Apache.

E.g. if you are using an AMP stack such as XAMP or MAMP for your server, and just want to use the default htdocs directory, you may want to create your web app inside its own directory under htdocs, and then set the URL to http://localhost/yourapp

Avoid a trailing slash at the end of the URL, as CodeIgniter 4's base_url() and site_url() functions forcibly return a URL without the trailing slash (as opposed to CodeIgniter 3) which nay lead to SEO-related issues if you are publishing to the Internet.

When you are done with the above, click 'Save...', and then you will be taken to the next screen where you can start adding modules to your new web application.

Screenshot of 'Creating a new web application' in CodeIgniter Wizard

MVC Modules in CodeIgniter Wizard

In CodeIgniter Wizard, the building blocks of your application are called modules and they consist of a model, an entity, a controller and view files for form and listing section of your modeled entities.

Screenshot of 'Modules list' in CodeIgniter Wizard

Creating a Module

To create a module, click the 'Create...' button at the bottom left of the modules list. You will then be presented with a modal window to define the components of your new module. Also make sure the database server is up and running during this process.

Generate MVC Code Module in CodeIgniter Wizard

At the top of the window, there are fields to specify the module name, single object name, plural object name, controller name, model name and entity name. These fields will have default values based on the table name.

CodeIgniter Wizard will smartly try to determine if the tables in your database have table prefixes and whether the table name is singular or plural, and then it will initially infer singular nouns for entity and model classes, and a plural form of the noun for controllers and the module name.

Although you can change these with whatever naming you like, it is recommended that you stick with the recommended pattern for the semantics of MVC paradigm.

Now select a database table from the pop-up menu at the top-left of this window.

This will read all the columns from the database and populate the table below with a matrix of fields to include or exclude in your model. Corresponding check-boxes at the beginning of each row indicate whether code should be generated for the given field. You can even specify column numbers for forms and ordering. At the end of each row, there are pop-up menus to chosse the form field type and input type.

If a database column has foreign keys defined in the database, the corresponding form field can be created as a dropdown <select> objects in the forms, instead of <input type="text">.

CodeIgniter framework has some form helper functions in PHP which contributes to creating clean and concise code for form views. CodeIgniter Wizard can create code for form pages using this feature, or it can create generic form objects using just HTML and PHP. The advantage of the former is clean but CodeIgniter-specific code, the advantage of the latter is that it would give you portable code most of which could also be used outside CodeIgniter. Therefore tick the checkbox labelled "Use back-end framework's own form helpers for view generation" if you want CodeIgniter-specific code.

'Form orientation in views' determines to choose between Bootstrap's vertical and horizontal form style. When horizontal is selected, "form-horizontal" CSS class added to the <form> tag and form labels are placed right to the left of input fields. In vertical orientation, labels are placed at the top of each input.

As of version 1.4, an ability to change the order of rows in this table by dragging up or down has been added. For this reason clicking in a text field inside a row to edit it takes about a second longer. That's because the app will first try to determine whether the click was for dragging the row or editing a text field.

When you are done with these settings, click "Generate Code" at the bottom-right. This will create code for model, views, controller and an entity linked to the model.

The generated code at this stage will not show up anywhere but will be stored in application data. In order to get them written to the file system, you will need to publish the module.

Editing a Module

Editing the module is mostly identical to creating it except that the database table will be pre-selected in the corresponding dropdown menu. All of your previous configuration will re-appear in the fields matrix.

Generate MVC Code Module in CodeIgniter Wizard
Coding Radio Buttons No Longer a Hassle Thanks to CodeIgniter Wizard

Coding radio buttons in HTML is a cumbersome task - especially when using a responsive layout with form controls grouped in nested div tags. Luckily CodeIgniter Wizard has facilitated their creation as of version 1.1.2. In order to be able to have them generated, you need to define a data source for them, though, either by entering static key-value pairs by yourself, or specifying a database table to dynamically generate multiple choices (to select one) for the end user. How to achieve this is described in the next paragraph.

Setting Up Dropdown Menus and (Input) Radio Buttons
Setting Up a Field to Pull Data from Another Database Table

The database table of your module might have foreign key references to joined tables in which case CodeIgniter Wizard will automatically generate the necessary code to bring data referring to many-to-one relationships so that if your module has a field with a foreign key referencing another table.

Alternatively you can use the "External Data Source" options to have dropdown select menus or input radio buttons generated in the form views of your web application.

Also in the 'Application Module Editor' window of CodeIgniter Wizard, dropdown menus under the columns 'referenced table', 'referenced PK' and 'choice label' within the generated fields table view will allow you to specify tables and columns to join without the need of a foreign key definition inside the database.

In cases where you chose 'Dropdown' or 'input' & 'radio' combination under the "Create as" column and checked the radio button labelled "From another DB table" under 'External Data Source', the wizard will add code to display a name instead of the PK / ID column, by forming an SQL query referring also to the joined tables, also assuming, however, the module for the external table - which in this case is 'Cities', will also have been created by you.

Currently CodeIgniter Wizard does not automatically generate modules for tables externally referenced to the primary table. You would need to ensure that related models and entities are present in the project by manually creating associated modules. The good news is, you can just hit the 'Create new module' button select the related database table in the dropdown menu on the upper-left, and then hit the 'Generate' button leaving everything else at their default setting - takes one minute or less - and that's all you need to do to create a module.

To summarize, if you have joined tables via foreign keys, and if you want the referenced fields automatically display a more human-friendly name than just their ID, you must make sure all the following conditions met:

  1. Make sure either 'Dropdown' or 'input' & 'radio' is selected next to field name under 'Created as'
  2. CodeIgniter wizard will automatically infer the ID of the referenced field, and it will make a guess which field will be the name field to refer to the related object (e.g. 'name', 'title', 'label', 'designation')
  3. If the wizard did not pick a good column / field for the name/title/label, you can manually pick the correct table column as the name field)
  4. When you are done with the module settings, click 'Generate Code and Close'. Next, create a module for the referenced table, and be sure not to exclude the field which will serve as the object's human-friendly name (e.g. 'name', 'title', 'label', 'designation', etc.)

Setting Up a Field to Display a List of Static Key Vale Pairs

You can have your dropdown menus or input-radio buttons display data from a static list you define. Just click the tiny square button next to "From static values..." under 'External Data Source' column, and enter your strings as key-value pairs in the pop-up menu that will show-up.

As much as it is intuitive, you can simply 'Save and Close' to have your settings applied. Please note that if you close the underlying 'application module' window without clicking 'Generate Code and Close', your manually entered static values will be discarded.

Unsupported MySQL Database Column / Field Types

CodeIgniter Wizard does not support some advanced field types such as "Geometry" and "BLOB" with a tiny exception: If you store images in a BLOB field (or one of its derivatives) and if your field name contains one of the words "photo", "picture" or "image", CodeIgniter Wizard will create an <img> tag with the src pointing to the BASE64 decoded binary data in list views only, when you use a standard (non-resource) controller. Aside from that, "include in forms" and "include in grid" checkboxes will be disabled for "Geometry" and "BLOB" (and their derivative) fields.

More Documentation

All this and more are available in the documentation accessible from the Help menu in the menubar of the CodeIgniter Wizard application.