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

Using Micro Application

Can we have a micro application section?

I want to use the micro framework in a more dynamic way, here is some code snippet from the documentation:

<?php

// With a function
function say_hello($name) {
    echo "<h1>Hello! $name</h1>";
}

$app->get('/say/hello/{name}', "say_hello");

// With a static method
$app->get('/say/hello/{name}', "SomeClass::someSayMethod");

// With a method in an object
$myController = new MyController();
$app->get('/say/hello/{name}', array($myController, "someAction"));

//Anonymous function
$app->get('/say/hello/{name}', function ($name) {
    echo "<h1>Hello! $name</h1>";
});

Notice that you can call functions from objects and static functions as well, my question is, how I can access the $app object and also how I can access the DI from inside those functions?

The defined Dirs, Namespaces and Classes under \Phalcon\Loader are available at that scope?

Thanks.

It should work $app->get('/say/hello/{name}', function ($name) use ($app, $di) { }



12.2k

Hi, thanks.

Yes that works indeed, but that is calling a lambda function.

I want to organize in a better way my code using classes, so I need to access $app or the DI object from my classes functions.



98.9k

Just create the controllers extending Phalcon\Mvc\Controller:

class MyController extends Phalcon\Mvc\Controller
{

    public function index()
    {
        echo 'This is the index';
    }

    public function show($id)
    {
        if ($this->request->isPost()) {
            return $this->response->redirect();
        }
        echo $id;
    }
}

$myController = new MyController();

$app->get('/', array($myController, 'index'));

$app->get('/show/{id}', array($myController, 'show'));

$app->handle();


12.2k
edited Oct '14

Hi Phalcon, that looks like does not work.

Here is my code:

index code:

// Starting the application
$app = new \Phalcon\Mvc\Micro();
$app->setDI($di);

$a = new \Modules\Search\Testing();
$app->get('/test/', array($a, 'show'));

$app->handle();

controller code:

<?php

namespace Modules\Search;

class Testing extends \Phalcon\Mvc\Controller
{
    public function show() {
       echo 'a';
        var_dump($this->request->isPost());
    }
}

The class function is getting call properly because I saw the 'a' I'm echoing. But I got this exception:

A dependency injection object is required to access the application services

That's using Phalcon 0.7.0

Tested using Phalcon 0.8.0, I got same exception.



98.9k

Ah ok, my code works on 0.9.0, then you need this:


$di = Phalcon\DI\FactoryDefault();

$app->setDI($di);

$myController = new MyController();
$myController->setDI($di);


12.2k

Yes, I was wondering about how to pass the DI.

Now it works.

Thanks.



12.2k

Now I'm wondering if there is a predefined way to access the DI using an static method?



98.9k
Accepted
answer

To access the DI statically:

$di = Phalcon\DI::getDefault();


12.2k

Perfect.

Thanks a lot.

Long live to Phalcon. ;-)