codeigniter_再见CodeIgniter,您好Laravel

codeigniter

A lot of PHP developers are fan boys of their PHP framework, and there are quite a few from which you can choose. Among the most popular ones are CodeIgniter, Zend Framework, CakePHP, Symfony, Yii, and the new kids on the block Silex, Slim, Lithium, and Laravel.

许多PHP开发人员都是其PHP框架的忠实拥护者,您可以从中选择很多。 其中最受欢迎的是CodeIgniter,Zend Framework,CakePHP,Symfony,Yii和Silex,Slim,Lithium和Laravel上的新孩子。

In the beginning of my career I stumble upon CodeIgniter and I love it for its simplicity, small footprint, and good documentation. It worked great for me when I developed a small to medium sized applications (usually Facebook apps), and the learning curve for new developers who worked with me was shallow enough that they could easily be integrated in the development process.

在我职业生涯的初期,我偶然发现了CodeIgniter,并且因为它的简单性,占用空间小和文档完善而喜欢它。 当我开发中小型应用程序(通常是Facebook应用程序)时,它对我非常有用,并且与我一起工作的新开发人员的学习曲线很浅,可以轻松地集成到开发过程中。

But last year, because of the Twitter buzz from some in the PHP community, blog posts, and the suggestions of some friends, I give Laravel 3 a try – and since that time I’ve never looked back. So, in this article I’d like to present a comparison of the two frameworks from my point of view.

但是去年,由于来自PHP社区的某些人的Twitter嗡嗡声,博客文章以及一些朋友的建议,我尝试了Laravel 3 –从那时起,我再也没有回过头。 因此,在本文中,我想从我的角度对两个框架进行比较。

要求 (Requirements)

CodeIgniter 2.1.3 needs PHP 5.1.6, but versions before 2 still worked with PHP 4. This was a drag since everybody moved on to use PHP 5. Laravel 3 needs PHP 5.3 and also the Mcrypt extension.

CodeIgniter 2.1.3需要PHP 5.1.6,但2之前的版本仍可用于PHP4。这是一个阻力,因为每个人都开始使用PHP5。Laravel 3需要PHP 5.3和Mcrypt扩展。

CodeIgniter 1 didn’t need Mcrypt, since if you didn’t have the extension installed then it would use a custom functions to encode/decode strings. Of course this slows down the application, and we were amazed at some of the production servers that didn’t have it installed.

CodeIgniter 1不需要Mcrypt,因为如果没有安装扩展,则它将使用自定义函数来编码/解码字符串。 当然,这会减慢应用程序的速度,我们对某些未安装该应用程序的生产服务器感到惊讶。

<?php
function encode($string, $key = '') {
    $key = $this->get_key($key);
    if ($this->_mcrypt_exists === true) {
        $enc = $this->mcrypt_encode($string, $key);
    }
    else {
        $enc = $this>_xor_encode($string, $key);
    }
    return base64_encode($enc);
}

休息 (REST)

It’s easy to build a REST API with Laravel using RESTful Controllers where you simply have the $restful property set true inside your controller. For example, if you’re building an API for a task manager, for updating a task it would use something like this:

使用RESTful控制器使用Laravel构建REST API很容易,您只需在控制器内部将$restful属性设置为true即可。 例如,如果要为任务管理器构建API,则要更新任务,将使用以下内容:

<?php
class Tasks_Controller extends Base_Controller {
    public $restful = true;
    public function get_index()
    {
    }
    public function put_update()
    {
    }
...
}

To handle PUT in CodeIgniter you would need this workaround:

要在CodeIgniter中处理PUT,您需要以下解决方法:

<?php
if ($_SERVER["REQUEST_METHOD"] == "PUT") {
    parse_str(file_get_contents("php://input"), $post_vars);
    $arData = json_decode(array_pop(array_keys($post_vars)));
}

As you can see, this is error prone and not very clean.

如您所见,这很容易出错并且不是很干净。

代码组织 (Code Organization)

Most of the time, the project you are working on will have a frontend and backend. In order to better organize the code in CodeIgniter, the best approach is to use HMVC. In this way you would have separate directories for backend code and frontend code so that different developers can work independently. To achieve something similar with Laravel, you can use nested controllers.

大多数情况下,您正在处理的项目将具有前端和后端。 为了更好地组织CodeIgniter中的代码,最好的方法是使用HMVC 。 这样,您将为后端代码和前端代码提供单独的目录,以便不同的开发人员可以独立工作。 要实现与Laravel类似的功能,可以使用嵌套控制器

风景 (The View)

CodeIgniter doesn’t have a default template engine, but you can easily integrate one like Smarty. On the other hand, Laravel uses Blade as its template engine which is very simple but powerful. With Blade, it’s very easy to set up a two-step view.

CodeIgniter没有默认的模板引擎,但是您可以轻松地集成Smarty之类的模板引擎。 另一方面,Laravel 使用Blade作为其模板引擎,这非常简单但功能强大。 使用Blade,很容易设置两步视图

You have a master template where usually you put the basic HTML structure with a header, navigation, footer, etc. Most of the time this template is called layout.blade.php or master.blade.php. The portion of code that will be injected from different different controllers is declared in the master template by @yield('content').

您有一个主模板,通常在其中放置基本HTML结构以及标头,导航,页脚等。大多数情况下,此模板称为layout.blade.phpmaster.blade.php 。 将从不同的不同控制器注入的代码部分在主模板中通过@yield('content')

<!DOCTYPE HTML>
<html>
 <head>
  ...
 </head>
 <body>
  <div class="header">
   ...
  </div>
  @yield('content')
 </body>
</html>

In custom templates declared per each method of a controller, for example a CMS page like “About Us”, you have a template called page.blade.php which looks something like this:

在按控制器的每种方法声明的自定义模板中,例如CMS页面(如“关于我们”),您有一个名为page.blade.php的模板,其外观如下所示:

@layout('master')
...
@section('content')
About page section
@endsection
...

You can have as many section as you like to declare and reuse.

您可以根据需要声明和重用任何部分。

A more detailed explanation about this can be found in the section Blade Layouts in the online open source book called Code Happy by Dayle Rees.

有关此问题的更详细说明,请参见在线开源书中名为Dayle Rees的Code Happy的“ 刀片布局 ”部分。

To achieve this in CodeIgniter, at least as far as I’ve been able to figure out, is to create a master controller which all other controllers extend and call a render method. A CodeIgniter start project for implementing two step views can be found under my GitHub account.

至少据我所知,要在CodeIgniter中实现此目的,是创建一个主控制器,所有其他控制器都将对其进行扩展并调用render方法。 在我的GitHub帐户下可以找到一个用于实现两个步骤视图的CodeIgniter启动项目

该模型 (The Model)

Codeigniter uses Active Record for database manipulation which is quite easy to master, but you can also use raw queries if you have complicated requirements. Laravel uses Eloquent as a ORM which is simple and works with major database servers. For writing queries, you can also use Fluent Query Builder, which looks like a lot like the Active Record approach. And of course, you can also write raw queries too.

Codeigniter使用Active Record进行数据库操作,这很容易掌握,但是如果您有复杂的要求,也可以使用原始查询。 Laravel使用机锋作为一个ORM这是简单的,并与主要的数据库服务器协同工作。 为了编写查询,您还可以使用Fluent Query Builder ,它看起来很像Active Record方法。 当然,您也可以编写原始查询。

命令行(Crons) (Command Line (Crons))

For Codeigniter to call a cron script, the best approach is to create a Cron controller. You also need to duplicate index.php and create a cron.php file right in the root directory, overriding the following the following $_SERVER values:

为了让Codeigniter调用cron脚本,最好的方法是创建一个Cron控制器。 您还需要复制index.php并在根目录中直接创建一个cron.php文件,覆盖以下$_SERVER值:

<?php
$_SERVER['SERVER_NAME'] = "xxxxxx.org";
$_SERVER['REQUEST_URI'] = "cron/index";
$_SERVER['QUERY_STRING'] = "lang=en";
$_SERVER['SERVER_PORT'] = "0";
$_SERVER['REQUEST_METHOD'] = "GET"

Then you can call it from the command line with php cron.php cron index.

然后,您可以从命令行使用php cron.php cron index调用它。

In Laravel it’s easier. You have Tasks using the command line tool that Laravel comes with called Artisan.

在Laravel中更容易。 您可以使用Laravel随附的称为Artisan的命令行工具来执行“ 任务”

(Miscellaneous)

  • Unit Testing – Codeigniter uses a very rudimentary testing class for writing unit tests. It is limited, but you can integrate PHPUnit. Laravel comes with PHPUnit out of the box, the most popular PHP unit testing framework.

    单元测试 – Codeigniter使用非常初级的测试类来编写单元测试。 它是有限的,但是您可以集成PHPUnit。 Laravel自带了PHPUnit,这是最受欢迎PHP单元测试框架。

  • Authentication – Codeigniter doesn’t have anything out of the box for authentication, but you can always use a third-party library, like Ion Auth. Larvel on the other hand has a basic auth library that you only need to configure before using.

    身份验证 – Codeigniter并没有开箱即用的身份验证功能,但是您始终可以使用第三方库,例如Ion Auth 。 另一方面,Larvel有一个基本的身份验证库 ,您只需要在使用前进行配置即可。

  • Caching – With CodeIgniter, the driver supports APC, Memcache, and the filesystem. Laravel on the other hand supports those and also database tables and Redis.

    缓存 –使用CodeIgniter,驱动程序支持APC,Memcache和文件系统。 另一方面,Laravel支持这些功能,还支持数据库表和Redis。

  • Hooks/Filters – If you want to step in and run certain code at different points of execution in CodeIgniter, you should do so using Hooks. Laravel offers Filters.

    挂钩/过滤器 –如果您想介入并在CodeIgniter中的不同执行点运行某些代码,则应使用挂钩 。 Laravel提供过滤器

  • Routing – Routing is done pretty much the same way in both Laravel and CodeIgniter, although I’ve found Laravel to be more flexible.

    路由 -尽管我发现Laravel更加灵活,但是在Laravel和CodeIgniter中路由几乎都是相同的。

  • Configuration – Configuration is done via predefined arrays stored in config files which support multiple environments in both frameworks.

    配置 –通过存储在配置文件中的预定义阵列来完成配置,该文件在两个框架中都支持多个环境。

  • Extensibility – Extending the core functionality of CodeIgniter is done with Libraries, and you can find a lot of them on Sparks (a package management system for CodeIgniter). Laravel uses Bundles, which might seem familiar to you if you’ve worked with Symfony 2. You can find check already-created bundles at bundles.laravel.com.

    可扩展性 –使用库完成了CodeIgniter核心功能的扩展,您可以在Sparks (CodeIgniter的程序包管理系统)上找到很多功能。 Laravel使用Bundles ,如果您使用过Symfony 2,您可能会觉得很熟悉。您可以在bundles.laravel.com上找到已经创建的检查包。

  • MigrationsDatabase migrations are specific for Laravel, a concept borrow from Ruby on Rails which is versioning at the database level.

    迁移数据库迁移特定于Laravel,Laravel是从Ruby on Rails借用的概念,该概念在数据库级别进行版本控制。

社区 (Community)

CodeIgniter used to have a bigger community, but many moved to different frameworks after EllisLab, the company behind it, dropped support and no new features were added. Some of those people joined the Laravel community which is now very active with a lot of tutorials, books, and even its first international conference.

CodeIgniter曾经有一个更大的社区,但是在其背后的公司EllisLab放弃支持并且没有添加任何新功能之后,许多人转向了不同的框架。 其中一些人加入了Laravel社区,该社区现在非常活跃,提供了许多教程,书籍,甚至是它的第一次国际会议。

The future seems to be very bright for Laravel since Laravel 4 is in beta 2 and its first conference has just ended. On the other side for CodeIgniter, there are few member contributors who so commit/merge patches into CodeIgniter 2 but there is no release date for CodeIgniter 3.

Laravel 4处于beta 2阶段并且其第一次会议刚刚结束,因此Laravel的未来似乎非常光明。 另一方面,CodeIgniter的成员贡献者很少将这些补丁提交/合并到CodeIgniter 2中,但没有CodeIgniter 3的发布日期。

结论 (Conclusion)

With Laravel 4 on the horizon, the framework definitely has the momentum now and the community behind it is very active. I’m kind of nostalgic when I talk about CodeIgniter, but it served me very well at the time. It taught me how to use MVC, was easy to learn, earned me my first money, and I even did my first book review about it, but as time went on it hardly changed. Maybe it’s just because it was very good at what it did? But web development changes rapidly, and new technologies come around, so if you want to survive you have to keep up.

随着Laravel 4的到来,该框架肯定会获得发展,其背后的社区也非常活跃。 当我谈论CodeIgniter时,我有点怀旧,但当时对我很有帮助。 它教会了我如何使用MVC的方法,易于学习,为我赢得了第一笔钱,甚至还对我进行了第一本书的评论,但是随着时间的流逝,它几乎没有改变。 也许仅仅是因为它做得很好? 但是网站开发变化Swift,并且出现了新技术,因此,要想生存就必须跟上潮流。

Just like in politics, everyone chooses a side. And for most of the time you can stick with your choice, but it’s always good to explore new things. For me, it meant goodbye CodeIgniter, hello Laravel.

就像在政治中一样,每个人都选择一方。 在大多数情况下,您可以坚持选择,但是探索新事物总是好的。 对我来说,这意味着再见CodeIgniter,您好Laravel。

Comments on this article are closed. Have a question about PHP frameworks? Why not ask it on our forums?

本文的评论已关闭。 对PHP框架有疑问吗? 为什么不在我们的论坛上提问呢?

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/goodbye-codeigniter-hello-laravel/

codeigniter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值