0

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();
}
user3140714
  • 85
  • 1
  • 9

1 Answers1

2

Woah there, hold on a second..

It is great that you've identified the code you've written could be improved, but improvement doesn't just mean "moving everything from one place to another".

What you have the chance to do is to rethink how you've done things, and move into a "more structured" (in my opinion..) way of working.

Assuming you have successfully got Phalcon setup, I'd suggest reading through the first tutorial. This really is a great resource.

The MVC architecture is something a lot of sites run with nowadays, and if you aren't familiar with it, this link might help you out.

It is difficult to provide a direct answer to your question, as I think after a bit of reading everything will become clearer.

In your case, it sounds like it might be good to have this Smarty class as a service, that can be registered with Phalcon that can then be dependency injected.

Have a go/read, and let me/us know how it goes for you.

Community
  • 1
  • 1
askrich
  • 598
  • 5
  • 20
  • +1 I would add 2 pence worth. Look into using the Phalcon autoloader & dependency injector so you don't need includes. Personally I see the global keyword as a last resort that I would only use if I have 3rd party code to coax into working, I don't need it in stuff I have written. Functions inside functions may work in other languages, and I have heard people claim they work in PHP but I think they are more trouble than they are worth – CodeMonkey Apr 09 '14 at 14:25