• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrofitting App to use CodeIgniter

#11
[eluser]sophistry[/eluser]
oh great!

i added a line of code to deal with GET queries. the view loading system was failing if faced with a GET query like this:

http://example.com/photos.php?id=3

i want this to be as much of a drop-in solution as possible so i added code to handle that situation. to let those types of queries past CI you have to set a couple of config settings - that is detailed in the code comments on the wiki.

cheers.

#12
[eluser]sophistry[/eluser]
dc, i just noticed you are using the chdir() function in your modified code.

i assume you are doing that because the CWD is actually the directory where CI starts off (where index.php is) rather than where the view is located. i think this is an interesting solution to potential path problems with old code in old sites that weren't designed to be "moved" to new locations. that is, pure relative paths in include() and require() calls get messed up.

for instance, if you have a script with
Code:
include('separate_file_in_same_directory');
PHP won't be able to find it. but if the clever programmer put in some "portability" code like this:
Code:
include(REAL_LOCATION_OF_THIS_FILE.'separate_file_in_same_directory');
then, moving it a new system like this site_migrate controller will have no problem dealing with it.

my only concern with using chdir() to the view file is that there may be other things the CI is doing after the $this->load->view() method that could potentially get confused if the CWD is changed. i am not sure about the implications, but i decided to solve the problem another way.

instead of using chdir() to help old scripts scope themselves, i just went in a modified the scripts.

however, your solution may be better in the long run. i'll try it out, but could you let me know if you get into any problems with chdir() yousrself? thanks.

#13
[eluser]dcunited08[/eluser]
Quote:instead of using chdir() to help old scripts scope themselves, i just went in a modified the scripts.

however, your solution may be better in the long run. i'll try it out, but could you let me know if you get into any problems with chdir() yousrself? thanks.

I have used it several times for different includes and requires and, so far, havn't had a problem. If I could go in and change every script, I would change it to use the library functions. The problem is with relative links here there and everwhere, there is no way for me to really do that. Say I have a file ./1.php and ./1/2.php and wanted both of them to look at ./1/3/2.php. They would need different includes from their relative locations, not something I would like to do with a search and replace. I put the second chdir in to return it to the original directory so that it does not have issues further down the line.

#14
[eluser]sophistry[/eluser]
ah, a second chdir() after the first! how polite... thanks for elucidating.

EDIT: how's this? EDIT2: removed conditionals on chdir() EDIT3: updated chdir() to use full server path to the file.

EDIT4: here's the link to the wiki with the full code and comments:
http://codeigniter.com/wiki/site_migrate/
Code:
<?php
class Site_migrate_base extends Controller {

    var $default_view_and_method = 'index';
    
    function Site_migrate_base()
    {
        parent::Controller();
    }
    
    function _remap()
    {
        $view_or_method = $this->uri->rsegment(2,$this->default_view_and_method);
        
        if (method_exists($this,$view_or_method))
        {
            $this->$view_or_method();
        }
        else
        {
            $local_view_path = rtrim($this->uri->ruri_string(),'/');
            $view_file = strtok($local_view_path, '?');
            $server_path_to_view = APPPATH.'views/';
            $view_file = (is_dir($server_path_to_view.$view_file)) ? $view_file.'/'.$this->default_view_and_method : $view_file;
            chdir(dirname($server_path_to_view.$view_file));
            $this->load->view($view_file);
            chdir(dirname(FCPATH));
        }
    }
}

/* End of file site_migrate_base.php */
/* Location: ./system/application/controllers/site_migrate_base.php */


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.