I want to go to Laravel instead of CodeIgniter 4, I want to tell CodeIgniter developers who are secretly planning to upgrade, the charm of Laravel

Aug 28, 2020 PHP CodeIgniter Beginner Laravel

Overview

Although I have done some work using CodeIgniter, I would like to introduce the world of Laravel, considering the engineer that I have no chance to touch Laravel because the recruiting framework at work is CodeIgniter. I thought I would write an article.

Uncle himself, Laravel is in a position of less than a month introductory, but he found the meaning in the point that **Because it is a beginner, it may be easy to write from a viewpoint that advanced users can not write ** I will.

First, let’s contrast the CodeIgniter and Laravel sources with a simple transcendental example.

The series is an irresponsible planning series that may or may not exist.

And this is Masakari’s welcome project (Please tell us a lot).

Source written in # CodeIgniter

First of all, I will write code that is simpler (but has a proper MVC structure).

A page that simply displays “Hello CodeIgniter!” when accessed with localhost.

Routing

$route['default_controller'] ='top';

Controller

class Top extends CI_Controller
{
    public function index()
    {
        // View Display data set
        $data = [
            'title' =>'title',
            'content' =>'text',
        ];

        // Screen display
        $this->load->view('Top/index', $data);
    }
}

Views

<!DOCTYPE html>
<html>
<head>
    <title><?= $title; ?></title>
</head>
<body>
    <h1>Hello CodeIgniter!</h1>
    <p><?= $content; ?></p>
</body>
</html>

Source written in # Laravel

What if you did the exact same thing with Laravel?

In conclusion, if you are a CodeIgniter experienced person, you can understand with almost no hurdles.

It’s a very simple example, but there are already some differences between CodeIgniter and Laravel.

Routing

Notice in Laravel that the method is specified.

Even in the same route, different classes and different functions can be assigned by methods (get / post etc.).

Actually, I don’t think it’s such a complicated usage, but strict distinction of methods is a characteristic of Laravel.

Route::get('/','TopController@index');

Controller

It is a simple controller that just outputs a view.

There are some minor differences.

Items CodeIgniter Laravel Remarks
Controller naming Top TopController Append Controller to the end
View path expression Top/index Top.index Separated by dots
View output $this->load->view(‘hoge’) view(‘hoge’) More concise

For some, Laravel has the impression that it’s pretty simple.

Simplity is the design concept of Laravel, so I’m sure there will be more and more surprises going forward.

class TopController extends Controller
{
    public function index()
    {
        // View Display data set
        $data = [
            'title' =>'title',
            'content' =>'text',
        ];

        // Show View
        return view('Top.index', $data);
    }
}

Views

Laravel uses a template engine called a “blade” to create views.

Notice that there is blade in front of the extension and it is index.blade.php.

<!DOCTYPE html>
<html>
<head>
    <title>{{ $title }}</title>
</head>
<body>
    <h1>Hello CodeIgniter!</h1>
    <p>{{ $content }}</p>
</body>
</html>

The great thing about Laravel is that it also offers an option to “don’t use” a “blade”.

In fact, instead of writing {{ $title }}, you can also write <?= $title; ?> in pure PHP code, but in practice, the “blade” was never used. It’s better.

One of the merits of the “blade” is that it will output **with htmlspecialchars() already applied (of course, it will be output without applying **). Methods are also available).

In CodeIgniter, I think it was necessary to use html_escape(......) every time.

** No style is required, but there are plenty of options that are convenient to use **, and even if you are a beginner, you will notice that safety measures are embedded ** This is a feature of Laravel.

#Summary

“If this example is too simple, it may not be very suitable as a material to explain the difference between CodeIgniter and Laravel.

Such worries ended in melancholy, and I was confident that I was able to convey the charm of Laravel from the first time.

Even so, the power of Laravel becomes more and more pronounced as the example specifications become more sophisticated.

If time and motivation allow, I would like to publish articles in this style using more advanced examples (eg screen drawing with database connection, architecture with asynchronous communication).