[英]Can we Integrate Laravel project as a library in CodeIgniter?
I want to increase the functionality of my CodeIgniter project by integrating some code that is written in laravel? 我想通过集成一些用laravel编写的代码来增加我的CodeIgniter项目的功能? how do I approach, Can I include the code via library in CodeIgniter ?
我该如何处理,我可以在CodeIgniter中通过库包含代码吗? If yes How?
如果是的如何? I only want to include controllers and ORM into the CI.
我只想将控制器和ORM包含在CI中。
Laravel code is a kind of api fetcher with function talks with other 3rd party services.
Laravel代码是一种api fetcher,具有与其他第三方服务的功能对话。
Yes you can use composer to install Laravel specific modules/projects, third-party projects in your CodeIginter. 是的,您可以使用composer在CodeIginter中安装Laravel特定模块/项目,第三方项目。 Just include
autoload
in your `index.php' file at top 只需在顶部的`index.php'文件中包含
autoload
// Composer autoload
require_once __DIR__.'/vendor/autoload.php';
I am using Eloquent
as ORM in my CodeIgniter codebase. 我在CodeIgniter代码库中使用
Eloquent
作为ORM。
Create a classmap to your app directory in composer.json
创建一个类映射到您的应用程序目录
composer.json
"autoload": {
"psr-4": { "YourApp\\": ["application/"] },
To use Eloquent
, you will require to create a library to setup Eloquent for use. 要使用
Eloquent
,您需要创建一个库来设置Eloquent以供使用。
/**
* 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. 同样,你可以用
MonoLog
的日志记录, Whoops
错误显示, Formers\\Former
的形式建设等。
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 您可以在
autload
之后将此代码放在某处并在index.php
定义CI环境以使用漂亮的https://github.com/filp/whoops库
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. 您还可以扩展
CI_Router
以在Code Igniter应用程序中使用Laravel样式路由。
You can extend the CI_Loader
to use Blade
templating in Code Igniter. 您可以扩展
CI_Loader
以在Code Igniter中使用Blade
模板。 Create a new file MY_Loader
in your application/core
directory with this code. 使用此代码在
application/core
目录中创建一个新文件MY_Loader
。
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. 您可能必须在
application/config
目录中创建配置文件blade.php
以存储blade
特定配置。
//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
));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.