Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Full App In Phalcon v4.0 Not Working!

I got micro app working as per this post [1] but full app that worked fine in phalcon v3 doesn't work anymore in v4. How can I use this same "Request" if it required by v4 to get this app going? Software Used: PHP v7.3.0, Phalcon v4.0.0-alpha1. This is the stracture and code I have now.

Structure:

|- test/
|--> app/
|-----> app/cache/ (volt cache files)
|-----> app/config/
|---------> app/config/bootstrap.php
|---------> app/config/routes.php
|-----> app/controllers/
|---------> app/controllers/Base.php
|---------> app/controllers/Index.php
|-----> app/views/
|---------> app/views/main.volt
|---------> app/views/menu.volt
|---------> app/views/hello.volt
|--> public/index.php
<?php // index.php

$debug = new \Phalcon\Debug();
$debug->listen();

require '../app/config/bootstrap.php';
echo $app->handle()->getContent();

// EOF: ./test/public/index.php
<?php // bootstrap.php

use Phalcon\Loader;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Application;

$ld = new Loader();
$di = new FactoryDefault();
$app = new Application($di);

$ld->registerNamespaces([
    'Test\Models' => '../app/models/',
    'Test\Libraries' => '../app/libraries/',
    'Test\Controllers' => '../app/controllers/',
])->register();

$di->set('dispatcher', function() {
    $dp = new Dispatcher();
    $dp->setControllerSuffix('');
    $dp->setActionSuffix('');
    $dp->setDefaultNamespace('Test\Controllers');
    return $dp;
}); #dispatcher

$di->set('view', function() {
    $view = new View();
    $view->setViewsDir('../app/views/');
    $view->registerEngines([
        '.volt' => function($view, $di) {
            $volt = new Volt($view, $di);
            $volt->setOptions([
                'compiledPath' => '../app/cache/',
                'compiledExtension' => '.html',
                'compiledSeparator' => '_',
                'stat' => true,
                'compileAlways' => true,
            ]); #options
            return $volt;
        } #volt
    ]); #engines
    return $view;
}); #view

$di->set('router', function() {
    $rt = new Router(false);
    require '../app/config/routes.php';
    return $rt;
}); #router

// EOF: ./test/app/config/boostrap.php
<?php //routes.php

$rt->notFound([
    'controller' => 'base',
    'action' => 'error'
]);

$rt->add('/', [
    'controller' => 'index',
    'action' => 'index'
]);

// EOF: ./test/app/config/routes.php
<?php // Base.php

namespace Test\Controllers;

class Base extends Controller {

  function initialize() {

    $this->view->web = 'http://test/';
    $this->view->cdn = 'http://cdn.test/';

  } #initialize

  function error() {
    exit('404');
  } #error

} #class

// EFO: ./test/app/controllers/Base.php
<?php // Index.php

namespace Test\Controllers;

class Index extends Base {

    function index() {

        $this->view->pick('hello');

    } #index

} #class

// EOF: ./test/app/controllers/Index.php

// menu.volt

<a href="{{web}}home">Home</a>
<a href="{{web}}about">About</a>

// main.volt

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Test</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="{{cdn}}styles.css">
    <script src="{{cdn}}scripts.js"></script>
  </head>
<body>
<div id="wrap">
{% include 'menu.volt' %}
{% block content %}{% endblock %}
</div> <!--wrap-->
</body>
</html>

// hello.volt

{% extends 'main.volt' %}
{% block content %}

<h1> Hello, Phalcon v4.0 </h1>

{% endblock %}

I get this following error:

BadMethodCallException: Wrong number of parameters
// ERROR LINE: echo $app->handle()->getContent();

Any help is appreciated. Thank You!

[1] https://forum.phalconphp.com/discussion/19169/phalcon-v40-doesnt-work



298
edited 11d ago

Just found this on [1] blog post. But how do I use this with above code example? Anyone!? There is no examples given O_O

Applications
The Phalcon\Mvc\Application, Phalcon\Mvc\Micro and Phalcon\Mvc\Router now must have a URI to process

[1] https://blog.phalconphp.com/post/upgrading-to-v4-alpha-1



3.7k
Accepted
answer

In index.php

echo $app->handle($_SERVER['REQUEST_URI'])->getContent();

or

$request = new Phalcon\Http\Request();
echo $app->handle($request->getURI())->getContent();


298

Thank you buddy, it works now. But why this change in v4 ?



3.7k

Not sure, probally because an application can also have other inputs then uri. For example a cmd-line application.



61.9k

It paves the way for simulating an entire request. Wow its really a lot of work but you could have a mock request, response and session service and then check that the output is correct.