Welcome Guest, Not a member yet? Register   Sign In
Migration Advice
#1

[eluser]richard_ctv[/eluser]
Hello.

I am in the process of planning the migration of two applications to CI. Both use 'legacy' code in that it mixes to a great degree php and html output. I am quite new to MVC frameworks but have been working an on app using CI for the last couple of weeks. It seems fairly simple to write something from scratch, but migration...

Both apps were written about 5 years ago, and require new features. I want to move them into CI as neither are MVC and adding new features means a lot more spaghetti code. And not having looked at the code for such a long time, I have to go through and work out how it all works! Probably better to do the migration and relearn (and clean up) the code as I go.

The structure of the main app is that it runs mostly of a single index.php file. You can read a full overview of the system in Linux Journal. The system is very stable and fast, just a bit messy to extend, hence the desire to move it to a framework. (We are about to move it to new faster hardware and update to php5 and mysql5, both from v4).

The app includes a whole bunch of files - for sessions, user authentication, cookies and access to objects in the database - and these are in directories below the site root. Fairly standard stuff. There is an additional large group of included files that contain classes for objects that represent items in the db. This is so I can go $object->print or $object->save and not have to worry about the underlying table differences.

There is also a complex query compiler that works in a similar way to the lucene query parser (although it pre-dates it), allowing complex search constructs. It also allows multiple data sources to be searched, regardless of the schema, and returned to the user in a simple format. This, I want to retain as it is what makes the system so powerful from the users' point of view (and represents a significant time investment).

I want to retain as much of the good code as possible, passing out data in arrays, as you do for MVC.

May main concern is how to move all the class files into CI in a structured and logical way, and then convert the descendant classes to be (i expect) libraries).

Other questions I have are: is search a library or a model. Ditto the object classes which access the DB.

Suggestions on restructuring this for CI and any other tips that come to mind would be appreciated!


regards,

Richard

======
basic structural overview:

root/htdocs/index.php
edit.php
help.php

root/includes/search.inc.php
other includes

root/includes/recordtypes/record.inc.php
other classes extended from record
#2

[eluser]Phil Sturgeon[/eluser]
After recently migrating several legacy PHP sites into CodeIgnitor, I found a very helpful way of doing things was to bung all of those nasty old function files into one big-ass helper and autoload it. That was as you go around replacing out old functions with CodeIgnitors various built-ins you can slowly strip this file down and split it up untill its gone.

As for files with mixed in PHP and HTML, put them in the controllers too. Its not practical MVC but its good to take it all one step at a time and work through untill its all correctly done.

For an example of a search function I put in one of my models, check this:

Code:
function search($keywords = '', $limit = 0, $offset = 0)
    {
        $this->db->select('gameID');
        $this->db->from('Games');
        
        if($this->input->post('q')):
            $keywords = $this->input->post('q');
        endif;
        
        // If its got spaces, split it up. If not then make an array from one entry
        $keywordArray = (strpos($keywords, ' ')) ? explode(' ', $keywords) : array($keywords);

        // Split up keywords and produce the LIKE query
        foreach($keywordArray as $keyword):
            echo $keyword;
            $this->db->orlike('gameName', $keyword);
            $this->db->orlike('description', $keyword);
        endforeach;
        
        if($this->where != '')         $this->db->where($this->where);
        if($limit > 0)                 $this->db->limit($limit, $offset);
        if($this->orderBy != '')     $this->db->orderby($this->orderBy);
        else                        $this->db->orderby('gameName', 'ASC');
        
            
        $query = $this->db->get();
        echo $this->db->last_query();
        $this->where = "";
        $this->orderBy = "";
        
        $data = array();

        foreach($query->result_array() as $row):
            $data[] = $this->get($row['gameID']);
        endforeach;

        $query->free_result();
        return $data;
    
    }

Have been meaning to convert it to a proper library so I dont have one of these in each of my models, but good enough for now.
#3

[eluser]richard_ctv[/eluser]
Thanks.

Most of the code is object based, but I like the way you got it going first using the old files and then moved things into CI bit by bit.

Luckily the old code worked under php5 with only about 30 changes (for 15,000 lines of code), so I can run the old version on the new server, and migrate a copy (which can access the same DB) to CI as time allows.

cheers,
Richard
#4

[eluser]Phil Sturgeon[/eluser]
You lucky bugger, the system im migtrating at the moment is only about 20 files and i've had to recode about 60% of it >.<

When will people learn that OOP is the way!
#5

[eluser]richard_ctv[/eluser]
[quote author="thepyromaniac" date="1184688685"]You lucky bugger, the system im migtrating at the moment is only about 20 files and i've had to recode about 60% of it >.<

When will people learn that OOP is the way![/quote]

Actually I'm quite lucky on two counts.

Firstly the original code is very OOP - I came from a Assembler/C/C++ background and most of it (even looking back at it now, IMHO) is not too bad.

Secondly, no object ever outputs HTML, even though the bits of the pages are assembled there - they always return a string which is output as required.

This has meant I have been able to set up a MY_Controller which sets up all the pre-existing objects, and then simply pass the output of various calls to the view.

Then, once I have the basic display working I will be moving the display logic out of the old files into views. Lastly I'll move whatever is left in the old objects into libraries or models as appropriate.

I am really impressed with CI - I looked at quite a few frameworks before picking it, and it is pitched just right. Some of the other frameworks just seemed like too much trouble, but I'm probably getting lazy in my old age. ;-)




Theme © iAndrew 2016 - Forum software by © MyBB