CodeIgniter 4 Modular SAS
May 12, 2022 by: AdminThis is a Structure Application Starter, to be used as base for any project
What is CodeIgniter Modular ?
CodeIgniter is a PHP full-stack web framework that is light, fast, flexible, and secure. More information can be found at the official site.
The “Modular Structure” allows to separate each module from other, having each one, controllers, database, filters, libraries, languages, models, routes, validations and views.
Modules | Module | Structure |
---|---|---|
Modules/ | Home/ | Config |
Controllers | ||
Language | ||
Views | ||
Modules/ | Users/ | Config |
Controllers | ||
Database | ||
Filters | ||
Language | ||
Libraries | ||
Models | ||
Validation | ||
Views | ||
Modules/ | Utils/ | Config |
Controllers | ||
Language | ||
Libraries |
This repository holds a composer-installable app starter, with jQuery, Bootstrap4, and Login/Register fully working module examples It has been built from the development repository.
More information about the plans for version 4 can be found in the announcement on the forums.
The user guide corresponding to this version of the framework can be found here.
Installation & updates
Go to a browsable directory (Example: c:\xampp\htdocs)
composer create-project xxperez/codeigniter4modular
then
cd codeigniter4modular
Updates
Whenever there is a new release of the framework:
composer update
When updating, check the release notes to see if there are any changes you might need to apply to your app
folder. The affected files can be copied or merged from vendor/codeigniter4/framework/app
.
Setup
Go to Codeigniter4Modular directory.
cd codeigniter4modular
Copy env
to .env
:
copy env .env
Edit .env, and tailor for your app, specifically the baseURL and default database settings:
app.baseURL = 'http://localhost/codeigniter4modular/public' database.default.hostname = localhost database.default.database = database database.default.username = username database.default.password = password database.default.DBDriver = MySQLi database.default.port = 3306
After database configuration, run migrate to ensure the table users are created:
php spark migrate -all
Modular configuration
Add this lines to your .env, and adapt as necessary. The example files have english and spanish translations:
#-------------------------------------------------------------------- # LANGUAGE #-------------------------------------------------------------------- app.defaultLocale = 'en' app.supportedLocales = ['en','es','fr','de'] app.negotiateLocale = true app.appTimezone = 'America/Chicago'
Now, you can test the app, browse your installed directory, click on “Login” in the main page.
http://localhost/codeigniter4modular
More about modular configuration
All modules will reside under /Modules, but can be allocated elsewhere. When your create a module, edit /app/Config/Autoload.php and add your module to the PSR4, to be able to be found.
public $psr4 = [ APP_NAMESPACE => APPPATH, // For custom app namespace 'Config' => APPPATH . 'Config', APP_NAMESPACE.'\Controllers' => APPPATH.'Controllers', 'Dashboard' => APPPATH . '../Modules\Dashboard' , 'Users' => APPPATH . '../Modules\Users' , ];
Each filter you declare inside a module, must to be aliased in app/Config/Filters.php to be able to be used inside a rule.
public $aliases = [ 'csrf' => \CodeIgniter\Filters\CSRF::class, 'toolbar' => \CodeIgniter\Filters\DebugToolbar::class, 'honeypot' => \CodeIgniter\Filters\Honeypot::class, 'auth' => \Users\Filters\AuthFilter::class, 'noauth' => \Users\Filters\NoAuthFilter::class, ];
Each validation you declare inside a module, must to be set in the ruleSets variable in app/Config/Validation.php.
public $ruleSets = [ \CodeIgniter\Validation\Rules::class, \CodeIgniter\Validation\FormatRules::class, \CodeIgniter\Validation\FileRules::class, \CodeIgniter\Validation\CreditCardRules::class, \Users\Validation\UserRules::class, ];
Modular libraries
Libraries within a module are used to define functions that lightweight the controllers. If you prefer to use validations inside a module library, you will need to access requests and validation objects directly:
$request = \Config\Services::request(); $validation = \Config\Services::validation(); $validation->setRules($rules, $errors); $validation->withRequest($request)->run(); $validationErrors = $validation->getErrors();
Modular creation
To create new modules, you can use spark module:create option, from base directory. For example, to create a module named ‘Customers’:
php spark module:create Customers
Module create options: -f ModuleDir (other than App/Modules) -c FCLMVO (Create only con[F]ig, [C]ontroller, [L]ibrary, [M]odel, [V]iew, [O]ther dirs)
php spark module:create Customers -c FCMV
Important Change with index.php
index.php
is no longer in the root of the project! It has been moved inside the public folder, for better security and separation of components.
This means that you should configure your web server to “point” to your project’s public folder, and not to the project root. A better practice would be to configure a virtual host to point there. A poor practice would be to point your web server to the project root and expect to enter public/…, as the rest of your logic and the framework are exposed.
Please read the user guide for a better explanation of how CI4 works! The user guide updating and deployment is a bit awkward at the moment, but we are working on it!
Repository Management
We use Github issues, in our main repository, to track BUGS and to track approved DEVELOPMENT work packages. We use our forum to provide SUPPORT and to discuss FEATURE REQUESTS.
This repository is a “distribution” one, built by our release preparation script. Problems with it can be raised on our forum, or as issues in the main repository.
Server Requirements
PHP version 7.2 or higher is required, with the following extensions installed:
Additionally, make sure that the following extensions are enabled in your PHP: