2

I want to include two files (routes.php and globals.php) in index.php (Fat Free Framework), but I don't know how.

I have tried require and include the Fat Free way:

$f3=require('lib/base.php');

$f3=require('app/globals.php'); 
$f3=require('app/routes.php');

$f3=include('app/globals.php');
$f3=include('app/routes.php');

and then just normal PHP:

require 'app/globals.php';
require 'app/routes.php';

but it does not work.

This works:

$f3->config('app/globals.cfg');
$f3->config('app/routes.cfg');

but I do not want to use .cfg files, just .php.

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
Liz
  • 189
  • 3
  • 10
  • Just out of curiosity, what is the problem with `cfg` files? – php_nub_qq Dec 28 '13 at 11:44
  • You don't have to overwrite the `$f3` variable each time you require/include a new script. `require('app/globals.php');` is enough. – xfra35 Dec 28 '13 at 14:17
  • Maybe you could elaborate on "it does not work"? – xfra35 Dec 28 '13 at 14:19
  • What do you want to include? PHP Class files or config files? for php classes use the AUTOLOAD var and set it to `app/`. – ikkez Dec 28 '13 at 19:41
  • You can use .php instead of .cfg just fine with nothing more than renaming the file to .php. I have one like... $f3->config('app/adminconfig.php'); – QuaffAPint Dec 29 '13 at 17:29

2 Answers2

2

If you are afraid of serving your .cfg or .ini files to the clients browser, you can simply forbid that in the shipped .htaccess file.

RewriteCond %{REQUEST_URI} \.cfg 
RewriteRule \.cfg$ - [R=404]
ikkez
  • 2,052
  • 11
  • 20
0

File and class inclusion

The Fat Free Way of including files is to use AUTOLOAD. This can be done in two ways:

(a) setting the AUTOLOAD variable in index.php:

$f3->set('AUTOLOAD', 'relativepathfromindex/routes.php; relativepathfromindexpath/globals.php');

(b) adding the AUTOLOAD variable to the config.ini file:

AUTOLOAD=relativepathfromindex/routes.php; relativepathfromindexpath/globals.php

then ensuring the config.ini file is referenced in index.php:

$f3->config('config.ini');

This is a great way to include all controllers for example in one go by adding something like the following to the config.ini:

AUTOLOAD=controllers/

Globals

From your file names however you appear to perhaps be using globals, which may include something like:

define("LOGLEVEL", "DEBUG");

The Fat Free Framework does not support this syntax, so use the same method described above using either a set in index.php (Fat Free recommends switching to CamelCase):

$f3->set('logLevel', 'DEBUG');

or adding it to config.ini:

logLevel=DEBUG

Routes

If you want to reference routes outside index.php, you will have to amend something like the following:

$f3->route('GET /@controller/@action','@controller->@action');

to a different format as you probably won't have $f3 declared:

Base::instance()->route('GET /@controller/@action','@controller->@action');
Community
  • 1
  • 1
SharpC
  • 6,974
  • 4
  • 45
  • 40