Welcome Guest, Not a member yet? Register   Sign In
CI2/3 and CI4 in parallel
#1

Let's say I have a site that is currently a combination of CI2 and pure php.  I will be working on upgrading the CI2 portion to the latest CI3, but as I understand that, it is all in place, such that I don't have to re-write the code to upgrade to CI3.  I've seen the upgrade path and it doesn't seem terribly difficult.
Now, let's say I want to start writing new stuff in CI4 until I can fully rewrite all of the existing code.
the existing CI code base is at www.example.com/path/to/project
assuming I install CI4 at /path/to/project/ci4, is there a way to do URL trickery such that, if I create a HelloWorld controller in CI4, the user could access it by going to:
www.example.com/path/to/project/helloworld and NOT have the 'ci4' portion in the URL path instead of www.example.com/path/to/project/ci4/helloworld ?
Thanks.
Reply
#2

To do so would require calling CI4 from the CI3 controller, which is probably not possible.
This is because it is impossible to run CI3 and CI4 at the same time because of the constants.

However, it would be possible to create your own index.php and have that script run CI3 or CI4.
The idea is to put your own router in front of CI3/4.
Reply
#3

I think I might have just figured this out using basic .htaccess rewrite.
In my CI2/3 root (/path/to/project), I added this to the top of my rewrite section:

Code:
  # CI4 controllers redirect to remove the 'ci4' from the URI
  RewriteCond %{REQUEST_URI} ^/path/to/project/helloworld.*
  RewriteRule ^(.*)$ /path/to/project/ci4/index.php?/$1 [L,NC,QSA]

Note that I had already moved my index out of the ci4/public dir and into the root ci4.
I will also have to add any new CI4 controllers as another rewritecond, but that will be fine for my case.
If I'm missing anything that could create issues, please let me know. This is my first time messing with rewrites.
Thanks.
Reply
#4

Are you able to share the session data between CI3 and CI4?
Reply
#5

(02-09-2024, 09:36 AM)mkganesh Wrote: Are you able to share the session data between CI3 and CI4?

I don't think there's an easy way to do it. In my CI4, prior to each page load, I am able to read my CI2/3 session info via the $_COOKIE global. Since my login/logout all happens in the old environment, I just have to read it in to check values and set up the CI4 session variables with what was in there. I delete them all prior to reading them in (in CI4 side) to make sure I'm getting the latest session data. I also had to ensure that my logout in CI2/3 was properly deleting all of the correct session information. In addition, in my case, I have to deal with data stored in the php $_SESSION global as well. it's a mess of a situation that I inherited, but I'm making it work as best I can. I haven't yet had a need to try to read CI4 session information into the CI2/3 side of things. I'm not sure if that would work the same or not.
Reply
#6

I don't know this works with CI3, but CI4 session can be used with plain PHP.
https://github.com/kenjis/ci4-session-in-plain-php

If you change the constant names like the following, it may work with CI3.
https://forum.codeigniter.com/showthread.php?tid=89311
Reply
#7

(This post was last modified: 02-22-2024, 10:12 PM by berky.)

(02-09-2024, 09:36 AM)mkganesh Wrote: Are you able to share the session data between CI3 and CI4?

So after some tinkering, I was able to come up with the following solution that works for me, but may not be (and probably isn't) the best way to do this.

Because I only cared about a specific session variable in CI3 side that could come from CI4 side, I figured out how to semi-manually pull in the session info on the CI3 side.

It's probably considered a shoddy way to do this, but hey, if it gets the job done....

anyway, I created a basic php file for import with 2 functions:

Code:
// function basically copied from PHP 'session_decode' page and slightly modified
function session_decode_to_array($session_string) : array {
    $current_session = session_encode();
    foreach ($_SESSION as $key => $value){
        unset($_SESSION[$key]);
    }
    session_decode($session_string);
    $restored_session = $_SESSION;
    foreach ($_SESSION as $key => $value){
        unset($_SESSION[$key]);
    }
    session_decode($current_session);
    return $restored_session;
}

function get_ci4_session_data () : array {

  // in my case, I'm storing session data in a file which seems to be the default for CI4
  // CI4 session ID from cookie ID (using default name)
  if (isset($_COOKIE['ci_session'])) {
    $session_id = $_COOKIE['ci_session'];
  } else {
    return [];
  }
  // this file is in a different directory structure, so point back to the CI4 session file
  $session_file = __DIR__ . "/../../v4/writable/session/ci_session$session_id";
  $session_encoded = file_get_contents($session_file);
  if ($session_encoded === false) {
    return [];
  }

  // grab array
  $ci4_session = session_decode_to_array($session_encoded);
  return $ci4_session;
}

Then, I basically imported the file where I need to use it and call the get_ci4_session_data function



(02-17-2024, 07:48 PM)kenjis Wrote: I don't know this works with CI3, but CI4 session can be used with plain PHP.
https://github.com/kenjis/ci4-session-in-plain-php

If you change the constant names like the following, it may work with CI3.
https://forum.codeigniter.com/showthread.php?tid=89311

Unfortunately this didn't work for my situation, but thank you for the info. I needed to view the session data inside CI3, so I came up with the 'hack' above.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB