• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get_instance function

#1
Hello,

I'm currently exploring the new CodeIgniter 4, like the whole new idea so i'm preppin my code into the new version 4.
Saw the form_helper is not implemented at this moment, so i've used to one from version 3 for my coding.

Then I saw the "get_instance" is not available, did this changed or is there something we need to call in order to use this?

Thanks in advance
Reply

#2
Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.
Best VPS Hosting : Digital Ocean
Reply

#3
(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.


Wow that's a huge improvement, thank you for your answer/knowledge!
Reply

#4
(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.

Could you please share any documentations or example regarding this issue? What is the process to assess a LIBRARY within HELPER?

Thanks in advance.
"Who thinks in code"
Perfectly describes who I am
mbparvez.me
Reply

#5
(08-15-2019, 06:47 AM)webdevron Wrote:
(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.

Could you please share any documentations or example regarding this issue? What is the process to assess a LIBRARY within HELPER?

Thanks in advance.

If you examine a CIv4 helper source file you should find several examples. Here are a couple from url_helper.php

PHP Code:
$url = new \CodeIgniter\HTTP\URI($fullPath);
//or for a "service"
$config = \CodeIgniter\Config\Services::request()->config

In some cases, you might find it helpful to include a "use" directive in a helper. For instance, in form_helper.php you find

PHP Code:
use Config\Services

And then later in the file this simple piece of code uses "services"

PHP Code:
$request Services::request(); 
Reply

#6
(08-15-2019, 10:02 AM)dave friend Wrote:
(08-15-2019, 06:47 AM)webdevron Wrote:
(10-16-2016, 08:31 AM)sv3tli0 Wrote: Autoloading removed the need of such method.
Now you have static service container which you can access to get DB or other service where you need it.
You can directly call objects which will be auto find.

Could you please share any documentations or example regarding this issue? What is the process to assess a LIBRARY within HELPER?

Thanks in advance.

If you examine a CIv4 helper source file you should find several examples. Here are a couple from url_helper.php

PHP Code:
$url = new \CodeIgniter\HTTP\URI($fullPath);
//or for a "service"
$config = \CodeIgniter\Config\Services::request()->config

In some cases, you might find it helpful to include a "use" directive in a helper. For instance, in form_helper.php you find

PHP Code:
use Config\Services

And then later in the file this simple piece of code uses "services"

PHP Code:
$request Services::request(); 


Could you please share any example of how to get CI 4 session value in a custom PHP file?

folder structure

- CI4
- htdocs
-- index.php <- index file of CI
-- folder test
---- test.php <- Here I need some CI session value

Thanks in advance
Reply

#7
For session in file.

PHP Code:
<?= session('item'); ?>

It is also chainable.

PHP Code:
<?php

$session 
session();

$tmp session()->item;
$tmp session()->get('item');

?>

Very easy with CodeIgniter 4.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#8
(12-23-2020, 10:06 PM)InsiteFX Wrote: For session in file.

PHP Code:
<?= session('item'); ?>

It is also chainable.

PHP Code:
<?php

$session 
session();

$tmp session()->item;
$tmp session()->get('item');

?>

Very easy with CodeIgniter 4.


In this case is session() a function that does not exist. Don't I have to load the session driver first? With Codeigniter 3 it was done this way
Code:
$CI =& get_instance();
$CI->load->driver('session');

How should that be done with CodeIgniter 4?
Reply

#9
session(); is a helper method.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#10
(12-24-2020, 01:16 PM)InsiteFX Wrote: session(); is a helper method.

Thx for your feedback but I'm getting this error

Fatal error: Uncaught Error: Call to undefined function session() in ....

How can I load CI session helper in a custom PHP file?

Code:
<?php

$session = session();
print_r($session);

?>
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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