diff --git a/ext/dispatcher.c b/ext/dispatcher.c index 246a1c72962..070563c8951 100644 --- a/ext/dispatcher.c +++ b/ext/dispatcher.c @@ -73,6 +73,8 @@ PHALCON_INIT_CLASS(Phalcon_Dispatcher){ zend_declare_property_string(phalcon_dispatcher_ce, SL("_defaultAction"), "", ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_string(phalcon_dispatcher_ce, SL("_handlerSuffix"), "", ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_property_string(phalcon_dispatcher_ce, SL("_actionSuffix"), "Action", ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_null(phalcon_dispatcher_ce, SL("_previousHandlerName"), ZEND_ACC_PROTECTED TSRMLS_CC); + zend_declare_property_null(phalcon_dispatcher_ce, SL("_previousActionName"), ZEND_ACC_PROTECTED TSRMLS_CC); zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_NO_DI"), 0 TSRMLS_CC); zend_declare_class_constant_long(phalcon_dispatcher_ce, SL("EXCEPTION_CYCLIC_ROUTING"), 1 TSRMLS_CC); @@ -982,7 +984,7 @@ PHP_METHOD(Phalcon_Dispatcher, dispatch){ PHP_METHOD(Phalcon_Dispatcher, forward){ zval *forward, *exception_message; - zval *namespace_name, *controller_name, *task_name, *action_name, *params; + zval *namespace_name, *controller_name, *task_name, *action_name, *params, *previous_controller_name, *previous_action_name; phalcon_fetch_params(0, 1, 0, &forward); @@ -993,6 +995,12 @@ PHP_METHOD(Phalcon_Dispatcher, forward){ phalcon_call_method_p1_noret(this_ptr, "_throwdispatchexception", exception_message); RETURN_MM_NULL(); } + + previous_controller_name = phalcon_fetch_nproperty_this(this_ptr, SL("_handlerName"), PH_NOISY_CC); + phalcon_update_property_this(this_ptr, SL("_previousHandlerName"), previous_controller_name TSRMLS_CC); + + previous_action_name = phalcon_fetch_nproperty_this(this_ptr, SL("_actionName"), PH_NOISY_CC); + phalcon_update_property_this(this_ptr, SL("_previousActionName"), previous_action_name TSRMLS_CC); /** * Check if we need to forward to another namespace diff --git a/ext/mvc/dispatcher.c b/ext/mvc/dispatcher.c index eba9246e59c..5458ddb2b1b 100644 --- a/ext/mvc/dispatcher.c +++ b/ext/mvc/dispatcher.c @@ -287,3 +287,25 @@ PHP_METHOD(Phalcon_Mvc_Dispatcher, getActiveController){ RETURN_MEMBER(this_ptr, "_activeHandler"); } +/** + * Returns the previous controller in the dispatcher + * + * @return string + */ +PHP_METHOD(Phalcon_Mvc_Dispatcher, getPreviousControllerName){ + + + RETURN_MEMBER(this_ptr, "_previousHandlerName"); +} + +/** + * Returns the previous action in the dispatcher + * + * @return string + */ +PHP_METHOD(Phalcon_Mvc_Dispatcher, getPreviousActionName){ + + + RETURN_MEMBER(this_ptr, "_previousActionName"); +} + diff --git a/ext/mvc/dispatcher.h b/ext/mvc/dispatcher.h index d5e36a45cde..86c35dc6f08 100644 --- a/ext/mvc/dispatcher.h +++ b/ext/mvc/dispatcher.h @@ -30,6 +30,8 @@ PHP_METHOD(Phalcon_Mvc_Dispatcher, _handleException); PHP_METHOD(Phalcon_Mvc_Dispatcher, getControllerClass); PHP_METHOD(Phalcon_Mvc_Dispatcher, getLastController); PHP_METHOD(Phalcon_Mvc_Dispatcher, getActiveController); +PHP_METHOD(Phalcon_Mvc_Dispatcher, getPreviousControllerName); +PHP_METHOD(Phalcon_Mvc_Dispatcher, getPreviousActionName); ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_mvc_dispatcher_setcontrollersuffix, 0, 0, 1) ZEND_ARG_INFO(0, controllerSuffix) @@ -53,6 +55,8 @@ PHALCON_INIT_FUNCS(phalcon_mvc_dispatcher_method_entry){ PHP_ME(Phalcon_Mvc_Dispatcher, getControllerClass, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_Dispatcher, getLastController, NULL, ZEND_ACC_PUBLIC) PHP_ME(Phalcon_Mvc_Dispatcher, getActiveController, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phalcon_Mvc_Dispatcher, getPreviousControllerName, NULL, ZEND_ACC_PUBLIC) + PHP_ME(Phalcon_Mvc_Dispatcher, getPreviousActionName, NULL, ZEND_ACC_PUBLIC) PHP_FE_END }; diff --git a/unit-tests/DispatcherMvcTest.php b/unit-tests/DispatcherMvcTest.php index 057d6533a63..468c528b5c1 100644 --- a/unit-tests/DispatcherMvcTest.php +++ b/unit-tests/DispatcherMvcTest.php @@ -164,7 +164,38 @@ public function testDispatcher() $dispatcher->dispatch(); $value = $dispatcher->getReturnedValue(); $this->assertEquals($value, "hello"); + } + + public function testDispatcherForward() + { + Phalcon\DI::reset(); + + $di = new Phalcon\DI(); + + //$di->set('response', new \Phalcon\Http\Response()); + + $dispatcher = new Phalcon\Mvc\Dispatcher(); + $dispatcher->setDI($di); + + $di->set('dispatcher', $dispatcher); + + $dispatcher->setControllerName('test2'); + $dispatcher->setActionName('index'); + $dispatcher->setParams(array()); + + $dispatcher->forward(array('controller' => 'test3', 'action' => 'other')); + + $value = $dispatcher->getControllerName(); + $this->assertEquals($value, 'test3'); + + $value = $dispatcher->getActionName(); + $this->assertEquals($value, 'other'); + + $value = $dispatcher->getPreviousControllerName(); + $this->assertEquals($value, 'test2'); + $value = $dispatcher->getPreviousActionName(); + $this->assertEquals($value, 'index'); } }