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.

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();
    $application->setDI($di);

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


868

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();
    $url->setBaseUri('/');
    return $url;
});

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

$di->setShared('session', function() {
    $session = new \Phalcon\Session\Adapter\Files();
    $session->start();
    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
    $application->registerModules(
        array(
            'frontend' => array(
                'className' => 'Multiple\Frontend\Module',
                'path'      => '../apps/frontend/Module.php',
            ),
            'backend'  => array(
                'className' => 'Multiple\Backend\Module',
                'path'      => '../apps/backend/Module.php',
            )
        )
    );
    $application->setDI($di);
    //Handle the request
    echo $application->handle()->getContent();

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

Thanks for your reply! S.



7.4k
Accepted
answer

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?



868

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