We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Upgrading to Phalcon 3 broke the existing site

My problem started right after I upgraded PHP to Version 7 and Phalcon to Version 3.

Problem

I m getting blank page, no error messages (Error is turned on), no `500 Internal server' error in console. The site used to work flawlessly before.

Is application made with previous version of phalcon not compatible with Phalcon 3?

I have following controller IndexController.php

namespace RealEstate\Property\Controllers;

use \Phalcon\Mvc\Controller;
use \Phalcon\Mvc\View;
use  RealEstate\Common\Models as CommonModels;

class IndexController extends Controller
{
   public function initialize(){
          //Code here
   }

   public function indexAction(){
       $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);

       $this->view->setVar("total_properties", $this->utils->getTotalProperties());    
       $this->view->pick("index");
       echo "HELLO WORLD";
   } 
}

The index action doesnot render anything, but yes HELLO WORLD is printed, so there seems there is no errors in code above that line.

My bootstrap index.php

namespace RealEstate;

use \Phalcon\Mvc\Application;
use \Phalcon\DI\FactoryDefault;
use \Phalcon\Loader;
use \Phalcon\Mvc\Router;
use \Phalcon\Mvc\View;
use \Phalcon\Mvc\Dispatcher;
use \Phalcon\Events\Manager     as EventManager;
use \Phalcon\Assets\Manager       as Assets;
use \Phalcon\Mvc\Url        as UrlProvider;
use \Phalcon\Db\Adapter\Pdo\Mysql   as DbAdapter;
use \Phalcon\Flash\Session      as FlashSession;
use \Phalcon\Session\Adapter\Files  as SessionAdapter;
use \Phalcon\Http\Response\Cookies; 

//use \Phalcon\Session\Adapter\Files  as Session;

class MyApplication extends Application
{

  const DEFAULT_MODULE = "property";
  private $prConfig;
  /**
   * Register the services here to make them general or register in the ModuleDefinition to make them module-specific
   */

  protected function _registerServices()
  {
    try{
      $config = include  "../apps/config/config.php";

      $di = new FactoryDefault();

      $loader = new Loader();

      /**
       * We're a registering a set of directories taken from the configuration file
       */
      $loader->registerDirs(
        array(
          __DIR__ . '/../apps/library/',
          __DIR__ . '/../apps/plugins/'
        )
      );

      $loader->registerNamespaces(array(
        'RealEstate\Common\Plugins' => '../apps/plugins/',
        'RealEstate\Common\Models' => '../apps/common/models/',
        'RealEstate\Library\Pagination' => '../apps/library/',
        'Facebook' =>  __DIR__.'/../apps/plugins/FacebookSDK/'
      ));

      $loader->registerClasses(array(
         "FacebookLib"   =>  __DIR__.'/../apps/library/FacebookLib.php',
         "Facebook" =>  __DIR__.'/../apps/plugins/FacebookSDK/autoload.php',
         "MobileDetect" => __DIR__.'/../apps/library/MobileDetect.php'
      ));

      $loader->register();

      $di->set('config', $config, true);

      //Register assets like CSS and JS
      //ASSETS REGISTRATION RELATED CODE COMES HERE

      $this->prConfig = $config;

      //register base url
      $di->set('baseurl', function() use ($config){
        $url = $config->application->baseUri;
        return $url;
      });

      //register CDN url

      //Module Information
      $di->set('appconfig', function() use ($config){
        $appconfig = $config->application;
        return $appconfig;
      });

      //Register URL helper
      //set base url
      $di->set('url', function() use ($config){
        $url = new UrlProvider();
        $url->setBaseUri($config->application->baseUri);
        return $url;
      });

      //Register dependency for a database connection
      $di->setShared('db', function() use ($config){
        return new DbAdapter(array(
          "host" => $config->database->host,
          "username" => $config->database->username,
          "password" => $config->database->password,
          "dbname" => $config->database->dbname
        ));
      });

      //Register and start session
      $di->setShared('session', function() {
          $session = new SessionAdapter();
          $session->start();
          return $session;
      });

      //Register custom library Utilities, so that it is available through out the application  
      $di->setShared("utils",function(){
        return new \Utilities();
      });

      //Registering a router
      $di->set('router', function() use ($config){

        $router = new Router(false);

        $controller = "index";
        /*Backend Routers Configuration Start*/

        $modules = $config->modules;

        $router->add('/', array(
            'module' => 'property',
            'namespace' => 'RealEstate\Property\Controllers\\',
            'controller' => 'index',
            'action' => 'index'
        ));

        $router->add('/:int', array(
            'module' => 'property',
            'namespace' => 'RealEstate\Property\Controllers\\',
            'controller' => 'index',
            'action' => 'index',
            'params' =>1
        ));

        //other router settings
        //404 route settings here

        $router->removeExtraSlashes(true); //ignore trailing slash in urls
        /*Backend Routers Configuration End*/
        return $router;

      });

     //partials related setting here

      $this->setDI($di);
    }catch(\Phalcon\Exception $e){
      echo get_class($e), ": ", $e->getMessage(), "\n";
        echo " File=", $e->getFile(), "\n";
        echo " Line=", $e->getLine(), "\n";
        echo $e->getTraceAsString();
    }

  }

  public function main()
  {

    try{
      if (!extension_loaded('phalcon')) {
            die("Phalcon extension is not installed or enabled. Please check with host");
      }

      $this->_registerServices();

      if($this->prConfig->application->environment=="development" || $_GET["showerrors"]==1){
        ini_set("display_errors","On");
        error_reporting(E_ALL ^ E_NOTICE);
      }

      $arraytemp = json_decode(json_encode($this->prConfig->modules), true); //convert Config object to a simple array
      //$this->utils->printr($arraytemp,1);
      $keys = array_keys($arraytemp);
      $array = array();

      if(count($keys)>0){
        foreach($keys as $module){
          $array[$module]["className"] = "RealEstate\\".ucwords($module)."\Module";
          $array[$module]["path"] = "../apps/modules/".$module."/Module.php";
        }
      }else{
        die("The entries for modules not found.");
      }

      $this->registerModules($array);
      echo $this->handle()->getContent();

    }catch(\Phalcon\Exception $e){
      echo get_class($e), ": ", $e->getMessage(), "\n";
        echo " File=", $e->getFile(), "\n";
        echo " Line=", $e->getLine(), "\n";
        echo $e->getTraceAsString();
    }
  }

}

$application = new MyApplication();
$application->main();
  1. No errors on Server's error log
  2. Also have checked on Phalcon 3 on PHP 5.6. Same result

Thanks



85.5k

double check all error logs you can find in /var/logs/ to see in which one exacly the error is placed. Cuz sometimes mine are placed at system.log or something like that ( i dont remmember exacltly )

Checked all error logs, no errors at all.

double check all error logs you can find in /var/logs/ to see in which one exacly the error is placed. Cuz sometimes mine are placed at system.log or something like that ( i dont remmember exacltly )



85.5k

perhaps apache/nginx is screwing with you ? check their logs if everything is fine.

if its working fine start removing peice by peice the code until it start working and find the code block that fucks it.

And then we will see. There are too many stuff to copy/paste it in blank app of mine to test it.

Did you try making a micro site to see if Phalcon is even running?

Micro app is working even without changing any previous codes.



6.6k
Accepted
answer
edited Aug '16

I think I solved this issue. The issue was in Module.php, the issue was in method

public function registerServices($di)
    {

        try{
            //Registering the view component
            $di->set('view', function() {
                $view = new View();
                $view->setViewsDir('../apps/modules/'.$this->module_name.'/views/');
                return $view;
            });

        }catch(\Phalcon\Exception $e){
            echo get_class($e), ": ", $e->getMessage(), "\n";
            echo " File=", $e->getFile(), "\n";
            echo " Line=", $e->getLine(), "\n";
            echo $e->getTraceAsString();
        }
    }

Where, $module_name is a property defined in class.

In above code $this->module_name was undefined. so I changed above method to -

public function registerServices($di)
    {
        $module = $this->module_name;

        try{
            //Registering the view component
            $di->set('view', function() use($module) {
                $view = new View();
                $view->setViewsDir('../apps/modules/'.$module.'/views/');
                return $view;
            });

        }catch(\Phalcon\Exception $e){
            echo get_class($e), ": ", $e->getMessage(), "\n";
            echo " File=", $e->getFile(), "\n";
            echo " Line=", $e->getLine(), "\n";
            echo $e->getTraceAsString();
        }
    }

I wondered why it was working in previous version of Phalcon

edited Aug '16

On previous to 3.0 when you used $this, on the closure used to define the service on the DIC, it was refering to the class where you was working on. Now, when you refer to $this inside the closure you are refering to the DI itself. Thats why. I had to change all my definitions too :(