- The current stable version is v.1.1.0
- Synopsis
- Folder Structure
- Useful Links
- Requirements
- What's Included
- Project Setup
- Install notes
- Other notes
- CLI Commands
- TODO's
- License
This is an MVC boilerplate for REST APIs powered by PHP, JWT, Composer and Codeigniter 3 with CLI based code generation. If you'd like to see the Postman docs for the provided Sessions
, Home
& JWT
examples, you can visit this link. To check out the official versioned releases of this repo, go here.
I primarily build APIs and I absolutely love working with the CodeIgniter framework and I wanted to keep the DRY KISS approach in all of my future API projects, while modernizing CodeIgniter 3 to allow it to be used for years to come with best practices at the forefront of boilerplate's design and structure. This is due to me wanting to eliminate the need to repeat tasks that are common when setting up an API focused 'micro-framework' using Codeigniter.
I built this as a small scaffold for my API based projects and tried making it as
un-opinionated as possible. It contains a number of cool features for REST
APIs such as a built-in JWT library, static routes and middleware brought to us by the amazing
Luthier-CI
package, a proper REST Controller library brought to us by Phil Sturgeon
and Chris Kacerguis, a small but useful MY_Controller
and an extensive
MY_Model
. Also included is a cool assortment of
composer dependencies and integration for libraries like Monolog
PSR3
Logger library, Dotenv by Vance Lucas for Environment configuration and
Whoops Errors for Cool Kids for API/UI errors during development as well as a
series of helper files, hooks and migrations to make the instantiation of REST
APIs far quicker and far more simple.
Please note, that this boilerplate is geared towards building only APIs and not initially built for User Interface mechanics (although that can still be done).
I hope everyone enjoys this and finds it useful. Please feel free to offer any advice or issue PRs and fixes where you see fit. All credit for the Codeigniter framework goes to the Codeigniter team at BCIT and credit for the composer dependencies goes to their respective authors. Without their work, this would've been a lot more difficult of a task =)
ROOT/
├── application/ # APPPATH directory
├── application/vendor/ # VENDORPATH directory
└── codeigniter/ # Codeigniter Framework
└── framework/
└── system/
├── application/composer.json
|
├── public_html/ # FCPATH directory
│ ├── web.config # Prebuilt web.config file for IIS servers
│ ├── .htaccess # Prebuilt .htaccess
│ ├── .env # Prebuilt .env file
│ ├── index.php # Custom index.php file
└──
- Composer Installation
- CodeIgniter Framework
- CodeIgniter Translations
- RESTful API.net
- REST API Tutorial.com
- CodeIgniter 3.1.10 Framework
- CodeIgniter REST Server
- Whoops Error Handler
- Standard Exceptions
- Php Dotenv Library
- Luthier-CI
- Monolog
- Faker
- A built-in
JWT
Library (See the example Controller, Routes & Docs) - A Powerful
MY_Model
class brought to us by Avenir - Code Generator for Controllers, Middleware, Models, Helpers, Migrations and Libraries
MY_Log
file forMonolog
PSR3 Logger Integration- A simple
MY_Controller
class to extend for basic REST usage - A revised
constants.php
with some cool, extra goodies - Luthier routing/middleware for Codeigniter 3
- Chris Kacerguis/Phil Sturgeon's REST Library
- Complete composer dependency control
- Most commonly used libraries/helpers are autoloaded
- Whoops Errors for Cool Kids integration
- PHP Dotenv library integration
- A series of robust helper files from CLI's to Databases
- Migration files for all of the database tables, including
ci_sessions
and the REST Library - Example Controller, Route, Model and Migration files to demonstrate the API
- Example Postman documentation for this is included here
- And much more... It is best to just go ahead and take a look!
$ cd path/to/application
$ composer install
$ cd path/to/application
$ composer update
$ cd path/to/public_html
$ php -S localhost:8080
Navigate to localhost:8080 to run the development server
- Clone or download this repo into your directory of choice
- Ensure that your web server 'points' to the
public_html
directory
- Do NOT access the
public_html
directory directly from your browser [EG -http:/mysite.com/public_html/
]. Always set your web server to 'point' to yourpublic_html
directory. This is done for security reasons.
$ cd path/to/application
and then run thecomposer install
command
If you want database sessions, and to use the included API examples:
- Go to
application/config/ENVIRONMENT/database.php
and enter your database credentials - Go to
application/config/ENVIRONMENT/config.php
and change the session type to database sessions located online 382
- Open your command line tool (EG - Git Bash) and run
$ cd path/to/public_html
then run the migration command$ php index.php luthier migrate
- Access your new API and Enjoy!
- Please read the docs of the Luthier-CI package if you've questions regarding routing and middleware. You can find the docs for that plugin here
- Update files manually that exist inside of the
application
folder as well as a modified version of theindex.php
file if it exists- Check the CodeIgniter User Guide for more information.
To execute the CLI commands simply run $ cd path/to/public_html
. Then use one of the following commands:
// Creating a controller:
$ php index.php luthier make controller ControllerName
// Creating a model:
$ php index.php luthier make model ModelName
// Creating a library:
$ php index.php luthier make library LibraryName
// Creating a helper:
$ php index.php luthier make helper HelperName
// Creating a middleware:
$ php index.php luthier make middleware MiddlewareName
// Creating a migration (by default, migrations are created by date)
$ php index.php luthier make migration create_table_users
$ php index.php luthier make migration create_table_users date
$ php index.php luthier make migration create_table_users sequential
To run migrations:
$ php index.php luthier migrate [version?=latest]
Where version is the version of the migration to run. If it's omitted, it will proceed to migrate to the latest available version.
Examples
$ php index.php luthier migrate
This will migrate to the latest available version
$ php index.php luthier migrate 20170706025420
This will run the 20170706025420_create_table_users
migration file
It's also possible to use one of these special values as version:
reverse
reverses ALL migrations
$ php index.php luthier migrate reverse
refresh
reverses ALL migrations and then proceeds to migrate to the latest available version
$ php index.php luthier migrate refresh
Alternately, you may simply import the SQL file located at APPPPATH/database/sql/rest_api.sql
into
your favorite RDBMS to get the desired tables.
A small list of things I would like to do by the version 2.0.0 release:
-
A small example using Codeigniter sessions.(Released in v1.0.0)Migration fileA controllerA modelRoutesDocs
-
Finish refactoring the migration files and get them working fully
- I am thinking that the
Luthier-CI
package is having issues when running migrations where all migrations do not run. i expect that this will be fixed for the 1.2.0 release
- I am thinking that the
-
An example representation of JWT authentication. This should be ready by the 2.0.0 release.
- Using an JWTAuthMiddleware
Copyright 2019 Jason Napolitano
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.