Welcome Guest, Not a member yet? Register   Sign In
Read/Load all php files with a certain ending?
#11

[eluser]Bramme[/eluser]
http://sandbox.bramme.net/benchmark

Not really impressive :p
#12

[eluser]Nummero2[/eluser]
Thanks you guys time for tryout :-)
#13

[eluser]Nummero2[/eluser]
Ok kind of confused

Heres what I did:

created a new script php file and put your code in it

Quote:$test123 = glob(APPPATH.'helpers/*_autoload_helper.php');
foreach($test123 as $key=>$test12)
{
$test123[$key] = str_replace('_helper.php','',substr(strrchr($test12,'/'),1));
}

$autoload['helper'] = $test123;

echo dump ( $test123 );

in my index.php view I created a new case for testing if my helpers are given as an array (thats why I put the echo dump statement there)

All I get is a blank screen, no error messages whatsoever but no "dumped" array of my helpers :-(

I looked further and realisied that I don´t have a _autoload_helper.php file in my helper folders which are two
- one located in 'xampp\htdocs\intranet\system\helpers',
- the other one in 'xampp\htdocs\intranet\system\application\helpers',

Don´t know am I understanding something wrong or did the guy who created the system delete the file?

Any help is appreciated


Greetings Sebastian
#14

[eluser]xwero[/eluser]
Where does the dump function come from? That is not a php function. If you want to see the array all you have to do is
Code:
print_r($test123);
or
Code:
var_dump($test123);

The $autoload[’helper’] = $test123; line is only effective if you put all of the code in the config/autoload.php file.
#15

[eluser]Nummero2[/eluser]
You mean its not a seperate script/function?

Its an extention to the autoload config file?

Thanks
#16

[eluser]xwero[/eluser]
No it's not an extension, it's an addition to the autoload file. Normally you have this
Code:
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|    $autoload['helper'] = array('url', 'file');
*/

$autoload['helper'] = array();
And if you add the code you get this
Code:
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|    $autoload['helper'] = array('url', 'file');
*/

$files = glob(APPPATH.'helpers/*_autoload_helper.php');
foreach($files as $key=>$file)
{
   $files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));
}

$autoload['helper'] = $files;
If you still want to manually add helpers you only have to do
Code:
$autoload['helper'] = array_merge($files,array('url','form'));
#17

[eluser]Nummero2[/eluser]
Ok thanks, will try.

But does that mean these helper files are always automatically loaded wheter I need them or not?
Because I only need to list them once in one class
#18

[eluser]xwero[/eluser]
if you only need them in one class you can change the _autoload part with the classnames so you know you only use them there and then you use the same code, except the $autoload['helper'] line in the contructor.

Code:
class Me extends Controller
{

   function Me()
   {
      parent::Controller();

      $files = glob(APPPATH.'helpers/*_me_helper.php');
      foreach($files as $key=>$file)
      {
           $files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));
      }

      $this->load->helpers($files);
   }

   // ...
}
#19

[eluser]Nummero2[/eluser]
Ok I did put this in the autoload.php config file:
Quote:$files = glob(APPPATH.'helpers/*_me_helper.php');
foreach($files as $key=>$file)
{
$files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));
}

$autoload['helper'] = array_merge($files,array('url','news'));

Then I created a new php file in the location I want it to be named me_helper.php. There I put the following in it:

Quote:class Me extends Controller
{

function Me()
{
parent::Controller();

$files = glob(APPPATH.'helpers/*_me_helper.php');
foreach($files as $key=>$file)
{
$files[$key] = str_replace('_helper.php','',substr(strrchr($file,'/'),1));
}

$this->load->helpers($files);


}


}

But now when i type:

var_dump('$files');

in the me_helper.php file under $this->load->helpers($files); I get nothing still i´m not given all my helpers from the folder xampp\htdocs\data\use\helpers
I only see a blank screen

I now I´m a noob but can someone help me? Please :-(
#20

[eluser]xwero[/eluser]
The class you put in the me_helper file has to be in the controller directory and you have to name it me.php.

To check if the code works you can add a file with the name a_me_helper.php in the helpers directory. Create an index function in your me.php with the following code
Code:
$files = glob(APPPATH.'helpers/*_me_helper.php');
foreach($files as $key=>$file)
{
$files[$key] = str_replace(’_helper.php’,’’,substr(strrchr($file,’/’),1));
}

var_dump($files);
An then you should see an array(a) appear on the page if you enter the site.com/index.php/me url. If that is true you know it works for the autoload.php file too. As an extra test you could add a b_me_helper.php and access the url again to see the string is now array(a,b)




Theme © iAndrew 2016 - Forum software by © MyBB