We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Dependency Injection in Controllers for POSTed values

Hi, cannot understand how to make the "automatic" filtering work on my controller. I have a Phalcon project with 2 different modules: Backend and Frontend. I configured it just like the example on the website and it works perfectly. In my public/index.php I added the following:

$di = new FactoryDefault();
    $di->set('filter', function() {
    return new \Phalcon\Filter();

I was sure this was enough to enable filtering in my controllers... But when from inside a controller I try to read variable that comes from a POSTed HTML form:

$issue->name = $request->getPost("name", "alphanum");

I get

A dependency injection object is required to access the 'filter' service

Anyone has any idea on what I am doing wrong? I can't find anything online. Thanks in advance, Simone

edited Mar '15

did you assign DI to your application in your bootstrap? like this:

    $application = new Phalcon\Mvc\Application();

    try {
        print $application->handle()->getContent();
    } catch (\Exception $e) {
        print $e->getMessage(); // or what you want to do....


Yes I did, and also all the other dependencies work, like the DB, or the session object:

I have this in the main index.php file

$di = new FactoryDefault();
$di->set('url', function(){
    $url = new Phalcon\Mvc\Url();
    return $url;

// Init filters
$di->set('filter', function() {
    return new \Phalcon\Filter();

$di->setShared('session', function() {
    $session = new \Phalcon\Session\Adapter\Files();
    return $session;
try {

    //Create an application
    $application = new Application($di);

    // Create the new configuration object
    $config = new \Phalcon\Config\Adapter\Ini($configFile);
    // Store it in the Di container
    $di->set('config', $config);

    // Register the installed modules
            'frontend' => array(
                'className' => 'Multiple\Frontend\Module',
                'path'      => '../apps/frontend/Module.php',
            'backend'  => array(
                'className' => 'Multiple\Backend\Module',
                'path'      => '../apps/backend/Module.php',
    //Handle the request
    echo $application->handle()->getContent();

} catch(\Exception $e){
    echo $e->getMessage();

Thanks for your reply! S.


I don't understand what wrong with your Phalcon!!!

what is your phalcon version?

try this one too: in your controller you should use

$this->request->getPost("name", "alphanum"); // instead of $request->getPost("name", "alphanum");

does it have a same error?


Thanks, this way it works! I think I'll switch my brain off and I'll use this second solution, Thanks!