2011-06-14 12 views
13

कमांडो को आपकी मदद की ज़रूरत है।नियंत्रक के तहत सभी कार्यों को नियंत्रित करने के लिए वाईआई की जादू विधि

मैं Yii में एक नियंत्रक है:

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

मैं सभी subrequest के तहत/पेज नियंत्रित करने के लिए Yii CController के तहत कुछ जादू विधि की जरूरत है || पेज नियंत्रक।

क्या यह वाईआई के साथ किसी भी तरह संभव है?

धन्यवाद!

उत्तर

19

ज़रूर है की जरूरत है। missingAction विधि को ओवरराइड करना सबसे आसान तरीका है।

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))); 
} 

आप बस जैसे के साथ बदलने सकता है:

यहाँ डिफ़ॉल्ट कार्यान्वयन है

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

ऊपर में, $actionID क्या आप $pageName के रूप में उल्लेख है।

थोड़ा और अधिक शामिल लेकिन अधिक शक्तिशाली दृष्टिकोण createAction विधि को ओवरराइड करना होगा। यहाँ डिफ़ॉल्ट कार्यान्वयन है:

/** 
* 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; 
    } 
} 

यहाँ उदाहरण के लिए, आप के रूप में भारी हाथ के कुछ के रूप में

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

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

कर सकता है या आप कुछ तरह से और अधिक व्यापक कर सकता है, अपनी आवश्यकताओं के अनुसार।

10

आपका मतलब है CController या नियंत्रक (आखिरी वाला आपकी विस्तारित कक्षा है)? इस तरह आप बढ़ाया तो CController वर्ग:

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

    //doSomeMagicBeforeEveryPageRequest(); 

    } 
} 

आप मिल सकता है क्या आप

संबंधित मुद्दे

 संबंधित मुद्दे