Welcome Guest, Not a member yet? Register   Sign In
Converting a CI3 Project to CI4.5.1
#1

(This post was last modified: 05-10-2024, 09:14 PM by Josh1985. Edit Reason: typo in topic )

Hello all!

I have recently sat down with the intention of converting a pretty large CI3 project into CI4. However, I have ran into a few issues that I can't seem to find in the documentation that really specifies what to do in my situation.

Library Conversions:

MyController.php (Custom Controller Library)
PHP Code:
<?php

 
class MyController extends CI_Controller{
  function __construct(){
  parent::__construct();
  $this->load->vars(array('toc' => $this->recipe_model->read_names_categories(),
  'admin_menu_items' => array(),
  'admin' => $this->is_logged_in()));
  if($this->is_logged_in()){
    date_default_timezone_set($this->session->userdata('tz'));
  }
  else{  // If the IF statement returns false, then  the ELSE statement tells php to do something else.
    date_default_timezone_set('America/New_York');
  }
  }
    
  
function is_logged_in(){
  $logged_in $this->session->userdata('logged_in');
  if(isset($logged_in) && $logged_in){
    return true;
  }
  else{  // If the IF statement returns false, then  the ELSE statement tells php to do something else.
    return false;
  }
  }
    
  
function require_login(){
  if(!$this->is_logged_in()){
    redirect('login');
  }
  }
    
  
function format_date($datestr){
  //date_default_timezone_set('UTC');
  $date strtotime($datestr.' UTC');
  //date_default_timezone_set('America/New_York');
  $datestr date("n/j/Y @ g:i A T"$date);
  return $datestr;
  }  
 
}  
 
 
// End of MyController CUSTOM Controller.php
 // End of MyController.php

?>

MySecurity.php (Custom Security Library)
PHP Code:
<?php

 
class MySecurity extends CI_Security {
  
  
/*
  * CSRF Set Cookie with samesite
  *
  * @codeCoverageIgnore
  * @return  CI_Security
  */
  
  
public function csrf_set_cookie(){
  $expire time() + $this->_csrf_expire;
  $secure_cookie = (bool) config_item('cookie_secure');
  if($secure_cookie && ! is_https()){
    return FALSE;
  }
  setcookie($this->_csrf_cookie_name,
            $this->_csrf_hash,
            ['samesite' => 'Strict',
              'secure'  => true,
              'expires'  => $expire,
              'path'    => config_item('cookie_path'),
              'domain'  => config_item('cookie_domain'),
              'httponly' => config_item('cookie_httponly')]);
              
  log_message
('info''CSRF cookie sent');
  return $this;
  }
 }

 
// End of MySecurity CUSTOM Security.php
 // End of MySecurity.php

?>

1. In CI3, this current project had several different custom libraries in use in CI3's application/core, CI3's application/libraries, and CI3's system/libraries directories. My question is here in CI4, I only see one "Libraries" directory in app/Libraries. Is this one location where ALL of my libraries would go? Or do I need to divide them between this directory and put others somewhere else?

2. In CI3, this project had a custom controller and a custom security exception.  So, again where would these files go?

3. I know in CI4, we often use BaseController as our extendable controller base class. The custom controller from CI3 that I am referring to was basically doing something very similar to what the BaseController class does in CI4. So my issue is, do I just add the custom parameters to BaseController or do I add the custom controller class into some special directory?   

Any help would be greatly appreciated!

Thanks
Reply
#2

(This post was last modified: 05-10-2024, 08:56 PM by ozornick.)

1. You can create any directory for new files. The main thing is to initialize them correctly in the code. 2. 3. Add general parameters to the BaseController or create your own extension based on it. +CSRF protection is already ready in ci4

See example https://github.com/neznaika0/codeigniter-expenses
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

(05-10-2024, 08:54 PM)ozornick Wrote: 1. You can create any directory for new files. The main thing is to initialize them correctly in the code. 2. 3. Add general parameters to the BaseController or create your own extension based on it. +CSRF protection is already ready in ci4

See example https://github.com/neznaika0/codeigniter-expenses

Thank you for your response.

However, I am still a bit confused by your answer.

I am aware I can add additional directories. However, the heart of my question was how does CI4 work with custom libraries? Is there a specific directory that the framework looks for those files in? Or can I use a "core" directory like before and CI4 work as it natively did in CI3 or do I have to code that link to the resources in for CI4? I am mainly asking if CI4 has any kind of library dependencies built-in for custom libraries, rather than the basic libraries that are built-in like: uri, form, database or etc.

In particular, would a controller library need to be defined as a normal controller or as a library resource?

Thanks again.
Reply
#4

Your MyController code would go into BaseController in CI 4.

As far as I can see your Security code has already been implemented into CI 4.

So you should only have to configure your Security config.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Basically, the framework is not linked to new directories. Everything you want to know is described in the documentation.
 But by default, everything is configured for folders that have already been created. Config, Controllers, Language.. All this can really change
Simple CI 4 project for beginners codeigniter-expenses
Reply
#6

(This post was last modified: 05-11-2024, 01:18 AM by kenjis.)

(05-10-2024, 09:30 PM)Josh1985 Wrote: I am aware I can add additional directories. However, the heart of my question was how does CI4 work with custom libraries? Is there a specific directory that the framework looks for those files in?

No.

CI4 has an autoloader followed by PSR-4 standard.
See https://codeigniter4.github.io/CodeIgnit...oader.html

Quote:By default, the namespace App is located in the app directory, and the namespace Config is located in the app/Config directory.

If you create class files in the locations and according to PSR-4, the autoloader will autoload them.
https://codeigniter4.github.io/CodeIgnit...namespaces
Reply
#7

(This post was last modified: 05-12-2024, 09:47 PM by Josh1985.)

(05-10-2024, 10:11 PM)InsiteFX Wrote: Your MyController code would go into BaseController in CI 4.

As far as I can see your Security code has already been implemented into CI 4.

So you should only have to configure your Security config.

Thank you for your response. Now I have a new issue:

Now that BaseController and my custom controller have been merged into one single controller, I now get the following error:

Error:
Code:
ErrorException

Undefined property: App\Controllers\Site::$load

The error references line 64 of BaseController.

Line 64 of BaseController:
Code:
$this->load->vars(array('toc' => $this->recipe_model->read_names_categories(),

Why does this line no longer work? I know normally CI4 does not use "load" anywhere in it's method calls.

However, when I looked for the remedy to my error in Google, the only thing I could find was a Codeigniter Forum Post that was dealing with variables being passed into a view. However, in my case, this line is responsible for reading the "recipe_model" for a query that looks in the database for my database table "recipe" with a field name of "category" and uses that data to build a "toc" view for my recipe nav.

What other way am I intended to do this in CI4? I have looked through the documentation for any reference to "$this->load->vars" and found nothing as an alternative.

Any ideas here?

Thank you in advance!

PS. I don't understand why things like this are not expressly spelled out in basic terms in the documentation. Especially, when most of these "load" errors feel a bit self inflicted with such a massive rewrite on the CI4 framework versus the behaviors in CI3 where "this->load->____" was literally everywhere throughout the framework. We all know "load" was a integral part of CI methods and calls until the advent CI4. Yes, things have to change from time to time but the lack of references to CI3 and the change in CI4 is remarkable. That being said, I do definately prefer using echo view('insertviewfilehere') much better than writing out this->load->view(insertviewfilehere) I do find it quite strange though that the documentation is written so heavily in code speak and not more in common language, or simply specifying something like "in CI3 things were done x way (include CI3 code snippet), now in CI4 we do things this this way (include CI4 code snippet)". This is particularly apparent in the "upgrading" sections of the documentation for each CI version or reiteration. Just my two cents on that, take that for what you will.
Reply
#8

(This post was last modified: 05-12-2024, 10:26 PM by ozornick.)

Sorry, are you too lazy for documentation? Smile  There are instructions for updating. I can't guarantee that everything is described, it's a big step.
https://codeigniter4.github.io/userguide...#libraries

I studied the CI3 documentation myself, fixed some scripts. Then I switched to CI 4. There were practically no problems with the migration.

You need to change your tactics. Not just copy the code, but reproduce the logic of the application.
For example, "load the model, output the model data" and not "create a variable to Copy/Paste replacing $load"
Simple CI 4 project for beginners codeigniter-expenses
Reply




Theme © iAndrew 2016 - Forum software by © MyBB