1

I have a router defined like this

$router->add("/api/:controller/:action/:params", array(
    'controller' => 1,
    'action' => 2,
    'params' => 3,
    'myParameter' => 'test'
));

So, I can get that "myParameter" parameter inside of controller like this

$this->dispatcher->getParam("myParameter");

But, I want to use this parameter in another class I call from Controller. Something like

myCustomClass::test();


class myCustomClass {
    function test() {
        // this is where I need that parameter
    }
}

Dispatcher returns nothing if I call it from myCustomClass.

(Of course getting the value inside of controller and passing it as a value is a solution, but I will probably use it at least for 100 different actions, so it is not really a good solution in this case).

Is there a way of getting that extra parameter outside of the scope of controller.

ahmetunal
  • 3,930
  • 1
  • 23
  • 26

2 Answers2

1

You need Default Dependency Injection, probably declared in index.php.

class myCustomClass {
    static function test() {
        $request = \Phalcon\DI::getDefault()->get( 'request' );
        $request->get("myParameter");
    }
}

Also you are missing STATIC word from function.

Erick Engelhardt
  • 704
  • 2
  • 10
  • 30
  • (sorry, I already had static in my code, forgot to paste it here), but still this one is giving me no result. Same behaviour with the shivanshupatel's answer. – ahmetunal Mar 31 '15 at 22:09
  • seems proper but I have strange feeling: shouldn't it be from `dispatcher` instead of `request`? – yergo Apr 01 '15 at 08:16
  • @yergo dispatcher control application flow, controller sequencing. Request accesses information from URL, GET, POST and others. – Erick Engelhardt Nov 21 '15 at 14:35
0

I am not 100% sure but I think you access get parameters from phalcon Http request class.

$request = new Phalcon\Http\Request();
$request->get("myParameter");
shivanshu patel
  • 782
  • 10
  • 19
  • 1
    tried that, it returns the actual request params (get and post). For this url, '/api/user/profile/1?x=y&a=b' $request->get() is Array ( [_url] => /api/user/profile/1 [x] => y [a] => b ) – ahmetunal Mar 31 '15 at 16:10