Neve12ende12
Neve12ende12

Reputation: 1194

Integrate Smarty Into Custom PHP MVC

My folder structure is as follows:

config/
controller/
core/
lib/
  smarty/
    All Smarty Files
model/
views/
index.php

My MVC directs all web traffic through index.php which includes the following code for autloading:

function __autoload($class){
  $file = sprintf('%smodel/%s.class.php',__SITE_PATH,$class);
  if(is_file($file)){
    include $file;
    return;
  }
  $file = sprintf('%smodel/%s.php',__SITE_PATH,$class);
   echo $file;
   if(is_file($file)){
    include $file;
    return;
  } 
}

Keeping in mind that you are being forced to use a version of PHP that does not support Namespacing or SPLAutoloading, how would you change the __autoload function or Smarty to play nice (work) with your website?

My specific problem is when smarty_internal_smartytemplatecompiler.php tries to include a file, it is looking at the model folder instead of the smarty folder. I suppose I could move Smarty to model/, but that doesn't seem "right."

Upvotes: 0

Views: 333

Answers (1)

bthecohen
bthecohen

Reputation: 486

Since you're using < 5.3, which doesn't support namespaces, are all the Smarty classes prefixed with "smarty_"? If so, you can just add a conditional to your __autoload() function that checks if the class name begins with "smarty_", and if so tells it to search the smarty folder:

if(strpos($class, "smarty_") === 0){
  $file = sprintf('%slib/smarty/%s.php',__SITE_PATH,$class);
  if(is_file($file)){
    include $file;
  return;
  } 
}

Upvotes: 1

Related Questions