include - Including files in index.php (Fat-Free Framework)

136

I want to include two files (routes.php andglobals.php) inindex.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.

258

Answer

Solution:

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]
825

Answer

Solution:

File and class inclusion

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

(a) setting theAUTOLOAD variable inindex.php:

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

(b) adding theAUTOLOAD variable to the :

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

then ensuring theconfig.ini file is referenced inindex.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 theconfig.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 inindex.php (Fat Free recommends switching to CamelCase):

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

or adding it toconfig.ini:

logLevel=DEBUG

Routes

If you want to reference routes outsideindex.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');

People are also looking for solutions to the problem: PHP Database take average and counter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.