Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rajankarthiks's avatar

Migrating existing codeigniter application to laravel

Dear members,

Now i have been assigned with a task for migrating a existing big Codeigniter application which is live. Now i have to take lead to migrate the legacy code into laravel with testing. how do i start this task? what are all the steps i need to follow.

the existing database is very ugly . how do i need to restructure the database, codebase, application architecture, etc. The current application has around 1 million active users. Please give suggestions and tips as well as steps that i need to follow?

0 likes
3 replies
thepsion5's avatar

If I were in your position, I'd start by slowly decoupling your application from codeigniter. Isolate the "business logic" type stuff from controllers and move it into their own classes that you instantiate inside the controller. Then, you need to do the same thing for that code, except with the database interactions. Once that's done, you can start to properly test it in isolation.

You have to essentially repeat that process for business logic of your application. While you're at it, I would strongly encourage you consider use entities and value objects that aren't tightly-coupled to the names of your database fields. For example:

interface ProductEntity
{

    public function getId();

    public function getDetails();

    public function getPrice();

    public function getStockLevels();
}

Now, make the interface work properly with your old CI model:

class ProductModel extends CI_Model implements ProductEntity
{

    var $poorlyNamedIdField;

    var $poorlyNamedPriceField;

    var $equallyWeirdTitle;

    var $alsoWeirdDescriptionField;

    /** 
     * If you change anything about this, the system will break everywhere at once
     * @author Developer Who Hates You
     * @var Array|ProbablyAnArray|Cthulu
     */
    var $newUpdated_correct_stockLevels;

    public function getId()
    {
        return $this->poorlyNamedIdField;
    }

    public function getDetails()
    {
        return new ProductDetails($this->equallyWeirdTitle, $this->alsoWeirdDescriptionField);
    }

    public function getStockLevels()
    {
        return new ProductStockLevel(
            $this->newUpdated_correct_stockLevels['total'],
            $this->newUpdated_correct_stockLevels['in_buffer'],
            $this->newUpdated_correct_stockLevels['available']
        );
    }

}

And your new Eloquent product model:

class EloquentProduct extends Eloquent implements ProductEntity
{
    protected $table = 'products';

    public function getId()
    {
        return $this->id;
    }

    public function getDetails()
    {
        return new ProductDetails($this->title, $this->description);
    }

    public function getStockLevels()
    {
        return new ProductStockLevel(
            $this->stock->total,
            $this->stock->shipping_buffer,
            $this->stock->available
        );
    }
}

Obviously, a real implementation will be harder, but you get the idea. The interface will let you refactor with your old implementation, but let yo use eloquent in your new one without rewriting everything that interacts with the models.

For stuff like services, you can do something similar by extracting them into a composer package. In that package, include a class that makes the service work with your current CI implementation, but also create a service provider designed to work with Laravel.

Basically, the whole process is slowly pulling your business logic away from the presentation/persistence/http layers of CodeIgniter. It'll be a long process, but easier than a full rewrite.

7 likes
ovidiu_dtp's avatar

This is a topic of great interest to me. I have a lot of legacy code in CI, and a lot of it is spaghetti code . I still have no clue how to refactor it Problem seems overwhelming at this point. I wish I could move it all in Laravel, but rewriting what I have is out of the question.

If there would be a way to move some of the code from CI in Laravel and keep a working app while doing so, it would be great, but I have no clue how to do it.

Another problem comes from the fact that my apps are in php 5.1, and most would probably break if php would be upgraded. I would need to run 2 php versions at the same time.

A hint on how to pick on this topic would be appreciated.

Please or to participate in this conversation.