Skip to content

Commit

Permalink
Merge pull request #1464 from dreamsxin/1462
Browse files Browse the repository at this point in the history
Fix #1462 Add new method for Phalcon\Mvc\Dispatcher
  • Loading branch information
Phalcon committed Oct 30, 2013
2 parents 41cd4d3 + 8b42364 commit df5b93a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ext/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions ext/mvc/dispatcher.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

4 changes: 4 additions & 0 deletions ext/mvc/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
};

31 changes: 31 additions & 0 deletions unit-tests/DispatcherMvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}

0 comments on commit df5b93a

Please sign in to comment.