My directory structure is something like below
> Root
> -Admin // admin area
> --index.php // admin landing page, it includes ../config.php
> -classes // all classes
> ---template.php
> ---template_vars.php // this file is used inside template.php as $template_vars = new tamplate_vars();
> -templates // all templates in different folder
> --template1
> -index.php
> -config.php
in my config.php file i have used
<?php
.... // some other php code
spl_autoload_register(NULL, FALSE);
spl_autoload_extensions('.php');
spl_autoload_register();
classes\template::setTemplate('template/template1');
classes\template::setMaster('master');
.... // some other php code
?>
I have set the proper namespaces (only in classes) and in my index.php on root I access the classes
<?php
require 'config.php';
$news_array = array('news1', 'news1'); // coming from database
$indexTemplate = new \classes\template('index');
$indexTemplate->news_list = $news_array; // news_list variable inside index template is magically created and is the object of template_vars class
$indexTemplate->render();
?>
So far it is working perfect, it renders the template and populate the template vars,
but when I open index file in admin folder, it gives following error
Fatal error: Class ‘classes\template_vars’ not found in
/home/aamir/www/CMS/classes/template.php on line 47
any idea how to fix this thing. It works on root, but from inside admin panel it doesnot work
You have to use a trick for that:
Before you include
../config.phpORInside
config.php