Pregunta

Comando necesita la ayuda de usted.

Tengo un controlador en yii:

class PageController extends Controller {
    public function actionSOMETHING_MAGIC($pagename) {
        // Commando will to rendering,etc from here
    }
}

Necesito algún método mágico en YII CController para controlar todo el subrequest en /Page || Controlador de página.

¿Es esto de alguna manera posible con YII?

¡Gracias!

¿Fue útil?

Solución

Seguro que lo hay. La forma más fácil es anular el missingAction método.

Aquí está la implementación predeterminada:

public function missingAction($actionID)
{
    throw new CHttpException(404,Yii::t('yii','The system is unable to find the requested action "{action}".',
        array('{action}'=>$actionID==''?$this->defaultAction:$actionID)));
}

Simplemente podrías reemplazarlo con EG

public function missingAction($actionID)
{
    echo 'You are trying to execute action: '.$actionID;
}

En lo anterior, $actionID es lo que te refieres como $pageName.

Un enfoque un poco más involucrado pero también más poderoso sería anular el createAction método en su lugar. Aquí está la implementación predeterminada:

/**
 * Creates the action instance based on the action name.
 * The action can be either an inline action or an object.
 * The latter is created by looking up the action map specified in {@link actions}.
 * @param string $actionID ID of the action. If empty, the {@link defaultAction default action} will be used.
 * @return CAction the action instance, null if the action does not exist.
 * @see actions
 */
public function createAction($actionID)
{
    if($actionID==='')
        $actionID=$this->defaultAction;
    if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method
        return new CInlineAction($this,$actionID);
    else
    {
        $action=$this->createActionFromMap($this->actions(),$actionID,$actionID);
        if($action!==null && !method_exists($action,'run'))
                throw new CException(Yii::t('yii', 'Action class {class} must implement the "run" method.', array('{class}'=>get_class($action))));
        return $action;
    }
}

Aquí, por ejemplo, podrías hacer algo tan pesado como

public function createAction($actionID)
{
    return new CInlineAction($this, 'commonHandler');
}

public function commonHandler()
{
    // This, and only this, will now be called for  *all* pages
}

O podría hacer algo mucho más elaborado, según sus requisitos.

Otros consejos

¿Te refieres a CController o controlador (el último es tu clase extendida)? Si extendió la clase CController como esta:

class Controller extends CController {
   public function beforeAction($pagename) {

     //doSomeMagicBeforeEveryPageRequest();

   }
}

Podrías obtener lo que necesitas

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top