This post may be long and messy but oh well... So i have my website in raw PHP
My original file structure:
/Index.php
/Users.php
/Smarty.class.php
/db.php
/Stats.php
/css/
/js/
Now i want to "port" each index file into phalcon controllers, like this:
/Controllers/IndexController.php
/Controllers/UsersController.php
/Smarty.class.php
/db.php
/css/
/js/
The thing is that global keyword doesnt work inside IndexController.php:
class IndexController extends \Phalcon\Mvc\Controller
{
include "/db.php"; // $db is initialized there
include_once ("Smarty.class.php");
$main_smarty = new Smarty;
public function indexAction()
{
function doSearch($limit) {
global $db, $current_user, $main_smarty; // db and smarty objects
$db->get_results("// my query"");
$search_clause = $this->get_search_clause();
$main_smarty->assign('search', $this->searchTerm);
}
}
}
Fatal error: Call to a member function get_results() on a non-object
But it works fine in my original code, without Phalcon.
include "/db.php";
function doSearch($limit) {
global $db, $current_user, $main_smarty;
$search_clause = $this->get_search_clause();
$main_smarty->assign('search', $this->searchTerm);
My bootstrap (Index.php) File:
try {
//Register an autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs(array(
'Controllers/',
))->register();
// DI
$di = new Phalcon\DI\FactoryDefault();
// View component
$di->set('view', function(){
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('/');
return $view;
});
$application = new \Phalcon\Mvc\Application($di);
echo $application->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo "PhalconException: ", $e->getMessage();
}