I want to increase the functionality of my CodeIgniter project by integrating some code that is written in laravel? how do I approach, Can I include the code via library in CodeIgniter ? If yes How? I only want to include controllers and ORM into the CI.
Laravel code is a kind of api fetcher with function talks with other 3rd party services.
Follow the steps mentioned below to create a custom library in CodeIgniter. The name of the library will be myLibrary. Steps to create a custom library in CodeIgniter. Assuming that you are aware of the directory structure of CodeIgniter. To create your own library, open application/libraries folder.
Researchers Segway the construction of the capstone project “Library Management System in Laravel Free Source Code” a platform that transforms the idea of library setup into a tiny and handy platform, where all of the existing processes in a library can also execute in this type of platform.
Once you have PHP and Composer installed, you can install Laravel. From a terminal window, enter the following command: composer global require "laravel/installer" export PATH=$PATH:$HOME/.composer/vendor/bin To start a Laravel project, run the following command.
As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries directory.
Yes you can use composer to install Laravel specific modules/projects, third-party projects in your CodeIginter. Just include autoload
in your `index.php' file at top
// Composer autoload
require_once __DIR__.'/vendor/autoload.php';
I am using Eloquent
as ORM in my CodeIgniter codebase.
Create a classmap to your app directory in composer.json
"autoload": {
"psr-4": { "YourApp\\": ["application/"] },
To use Eloquent
, you will require to create a library to setup Eloquent for use.
/**
* Capsule setting manager for Illuminate/database
*/
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
class Capsule extends CapsuleManager {
public function __construct()
{
parent::__construct();
//Loaded by CI
if(function_exists('get_instance')) {
$ci = &get_instance();
$db = new stdClass;
$db = $ci->db;
} else {
require_once __DIR__.'/../config/database.php';
$db = (object) $db['default'];
}
$this->addConnection(array(
'driver' => $db->dbdriver,
'host' => $db->hostname,
'database' => $db->database,
'username' => $db->username,
'password' => $db->password,
'charset' => $db->char_set,
'collation' => $db->dbcollat,
'prefix' => $db->dbprefix,
));
$this->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$this->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$this->bootEloquent();
}
}
// END Capsule Class
Now load the auto load the library, and you have the eloquent beauty.
Similarly, you can use MonoLog
for logging, Whoops
for error display, Formers\Former
for form building etc.
You can place this code somewhere after autload
and defining CI Environment in your index.php
to use beautiful https://github.com/filp/whoops library
if (ENVIRONMENT == 'development') {
$whoops = new \Whoops\Run;
$whoops->pushHandler(new Whoops\Handler\PrettyPageHandler());
$whoops->register();
}
You can also extend CI_Router
to use Laravel style routing in your Code Igniter app.
You can extend the CI_Loader
to use Blade
templating in Code Igniter. Create a new file MY_Loader
in your application/core
directory with this code.
use Illuminate\Blade\Environment;
use Illuminate\Blade\Loader;
use Illuminate\Blade\View;
class MY_Loader extends CI_Loader {
public function __construct()
{
parent::__construct();
}
public function blade($view, array $parameters = array())
{
$CI =& get_instance();
$CI->config->load('blade', true);
return new View(
new Environment(Loader::make(
$CI->config->item('views_path', 'blade'),
$CI->config->item('cache_path', 'blade')
)),
$view, $parameters
);
}
}
You may have to create a config file blade.php
in your application/config
directory to store blade
specific configurations.
//config/blade.php
$config['views_path'] = APPPATH . 'views/blade/';
$config['cache_path'] = APPPATH . 'cache/blade/';
Now you can do something like this in your controller
class Home extends CI_Controller {
public function index()
{
// Prepare some test data for our views
$array = explode('-', date('d-m-Y'));
list($d, $m, $y) = $array;
// Basic view with no data
echo $this->load->blade('home.index');
// Passing a single value
echo $this->load->blade('home.index')->with('day', $d);
// Multiple values with method chaining
echo $this->load->blade('home.index')
->with('day', $d)
->with('month', $m)
->with('year', $y);
// Passing an array
echo $this->load->blade('home.index', array(
'day' => $d,
'month' => $m,
'year' => $y
));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With