我们可以将Laravel项目集成为CodeIgniter中的库吗? [英] Can we Integrate Laravel project as a library in CodeIgniter?
问题描述
我想通过集成一些在laravel中编写的代码来增加我的CodeIgniter项目的功能?我如何处理,
我可以通过库在CodeIgniter包括代码?如果是如何?
我只想将控制器和ORM包含在CI中。
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代码是一种具有功能的api fetcher与其他
第三方服务对话。
Laravel code is a kind of api fetcher with function talks with other 3rd party services.
推荐答案
您可以使用composer在您的CodeIginter中安装Laravel特定的模块/项目,第三方项目。只需在顶部的index.php文件中包含 autoload
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';
我使用 Eloquent
CodeIgniter代码库。
I am using Eloquent
as ORM in my CodeIgniter codebase.
在 composer.json
中创建一个类映射到您的应用目录
Create a classmap to your app directory in composer.json
"autoload": {
"psr-4": { "YourApp\\": ["application/"] },
使用Eloquent
使用<$ c $
Use Eloquent
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
同样,您可以使用 MonoLog
进行日志记录, Whoops
用于错误显示, Formers\Former
用于表单构建等。
Similarly, you can use MonoLog
for logging, Whoops
for error display, Formers\Former
for form building etc.
您可以将此代码放置在 autload
之后的某个位置, code> index.php 使用美观的 https://github.com/filp / whoops 库
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();
}
您也可以扩展 CI_Router
在您的代码Igniter应用程序中使用Laravel样式路由。
You can also extend CI_Router
to use Laravel style routing in your Code Igniter app.
code> CI_Loader 在代码Igniter中使用刀片
模板。使用此代码在您的应用程序/ core
目录中创建一个新文件 MY_Loader
。
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
);
}
}
您可能需要创建一个配置文件 application / config
目录中存储刀片特定配置的<$ c> blade.php
。
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
));
}
}
这篇关于我们可以将Laravel项目集成为CodeIgniter中的库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!