Yii2 Framework Essay 3

Source: Internet
Author: User

Start reading vendor/yiisoft/yii2/base/action.php today

<?PHPnamespace yii\base;//namespace Useyii;//Loading the yii.php under the Yii folder/** * action is the base class for all controller Action classes. * Action provides a-to Reuse action method code. An action method in a action * class can be used in multiple controllers or in different projects. * * Derived classes must implement a method named ' Run () '. This method * would be invoked by the controller when the action is requested.  * the ' run () ' method can have parameters which would be filled up * with user input values automatically according to their Names.  * For example, if the ' run () ' method is declared as follows: * * "PHP * Public Function" Run ($id, $type = ' book ') {...} * "*" * and the parameters provided for the action is: ' [' id ' = 1] '. * Then the ' run () ' method would be invoked as ' run (1) ' automatically. * * @property string $uniqueId The unique ID of this action among the whole application. This property is * read-only. * * @author Qiang Xue <[email protected]> * @since 2.0 */
/* This controller is the base class for all controller actions.
This controller provides a way for you to reuse the code of the action method, which can be used in multiple controllers or in different projects.
Derived classes must implement a method named Run (), which is called when the controller is requested.
For example: the run () invocation declaration is as follows: Public function run ($id, $type = ' book ') {} and provides the arguments for the operation [' ID ' =>1];
So when run (1), the parameter is 1, the run () is called automatically;
This property is read-only.
*/

This class inherits the component class, so let's take a look at what the component class is. (Please go to the next section of code)classActionextendscomponent{/** * @var string ID of the action*/ Public $id; /** * @var Controller|\yii\web\controller The Controller that owns this action*/ Public $controller; /** * Constructor. * * @param string $id The ID of this action * @param controller $controller The controller, the owns this action * @param array $config name-value pairs that'll be used to initialize the object properties*/ Public function__construct ($id,$controller,$config= []) { $this->id =$id; $this->controller =$controller; Parent:: __construct ($config); } /** * Returns the unique ID of this action among the whole application. * * @return string The unique ID of this action among the whole application. */ Public functionGetuniqueid () {return $this->controller->getuniqueid (). ‘/‘ .$this-ID; } /** * Runs This action with the specified parameters. * This method was mainly invoked by the controller. * * @param array $params the parameters to being bound to the action ' s run () method. * @return Mixed The result of the action * @throws invalidconfigexception if the action class does not has a run () Me Thod*/ Public functionRunwithparams ($params) { if(!method_exists($this, ' Run ')) { Throw NewInvalidconfigexception (Get_class($this) . ' must define a ' run () ' method. '); } $args=$this->controller->bindactionparams ($this,$params); Yii:: Trace (' Running action: '.Get_class($this) . ':: Run () ',__method__); if(Yii::$app->requestedparams = = =NULL) {Yii::$app->requestedparams =$args; } if($this-Beforerun ()) { $result=Call_user_func_array([$this, ' Run ',$args); $this-Afterrun (); return $result; } Else { return NULL; } } /** * This method was called right before ' run () ' is executed. * You may override the ' this ' method to ' do preparation ' for the action run. * If The method returns false, it'll cancel the action. * * @return Boolean whether to run the action. */ protected functionBeforerun () {return true; } /** * This method was called right after ' run () ' is executed. * You may override the ' this ' method to ' do post-processing ' for the action run. */ protected functionAfterrun () {}}

Let's start with a little bit more on component.php.

The directory is: vendor/yiisoft/yii2/base/component.php

<? PHP namespace Yii\base;//Namespace  Use  Yii; Loading the related classes

/**
* Component is the base class that implements the *property*, *event* and *behavior* features.
*
* Component provides the *event* and *behavior* features, in addition to the *property* feature which are implemented in
* Its parent class [[Object]].
*
* Event is a-to-"inject" custom code into existing code at certain places. For example, a Comment object can trigger
* an ' add ' event when the user adds a comment. We can write custom code and attach it to this event so the the event
* is triggered (i.e comment would be added) and our custom code would be executed.
*
* An event was identified by a name this should be unique within the class it was defined at. Event names is *case-sensitive*.
*
* One or multiple PHP callbacks, called *event handlers*, can be attached to an event. You can call [[Trigger ()]] to
* Raise an event. When a event is raised, the event handlers'll be invoked automatically in the order they were
* attached.
*
* To attach a event handler to a event, call [[on ()]]:

*/
A component is a base class that implements attributes, events, and behavior characteristics.
Component provides event behavior, which is characteristic of inheriting his parent class object.

An event is an "injection" of custom code into an existing code in a specific place. For example
When a user adds a comment event, the annotated object can be triggered. We can write custom code and attach it to this event so that when the event is triggered (that is, the comment is added),

Our custom code will be executed,

One or more PHP callbacks, referred to as ' event handling '. To trigger an event handling, you can use on (); for example.

* "PHP"
* $post->on (' Update ', function ($event) {
*//Send email notification.
* });

In the above example, an anonymous function is connected to the ' Update ' event.

You can do this with the following types of programs:

*-anonymous function: ' function ($event) {...} '------anonymous function
*-Object method: ' [$object, ' handleadd '] '------object methods

*-Static Class method: ' [' Page ', ' handleadd '] '------static classes methods
*-Global function: ' Handleadd '------globals


classComponentextends Object{ /** * @var array The attached event handlers (event name = handlers)*/
An event handler that is an array. (Event name = = handler)
Private $_events= []; /** * @var behavior[]|null the attached behaviors (Behavior name = Behavior). This is ' null ' if not initialized. */
The connection form when the behavior is empty, (behavior name = = behavior), NULL when uninitialized.
Private $_behaviors; /** * Returns The value of a component property. * This method would check in the following order and act accordingly: * *-a property defined by a Getter:return The Getter result *-a property of a Behavior:return the Behavior property value * * Don't call this meth OD directly as it's a PHP magic method that * would be implicitly called when executing ' $value = $component->prope Rty, '. * @param string $name The property name * @return Mixed the property value or the value of a behavior ' s property * @throws Unknownpropertyexception If the property isn't defined * @throws invalidcallexception if the property is WRI Te-only. * @see __set ()*/
  

/ **
* Returns the property value of a component.
* This method will check the following sequence and take the appropriate action:
*
*-Properties defined by a getter: Returns the result of the Getter
*-a property of a behavior: the value of the Behavior property returned
*
* Do not call this method directly, because it is a PHP magic method
* Will be executed '

$value = called when $component->property;

'.
* @ Parameter string $name property name
* @ Returns the value of a property of a mixed property value or behavior
* @throws unknownpropertyexception If no attribute is defined, the message is thrown.
* @throws Invalidcallexception If the property is write-only.
* @see__set ()


Public function__get ($name) { $getter= ' Get '.$name;//Definition $getterif(method_exists($this,$getter) {//If $getter This method is called in this object, it returns True. //read property, e.g. GetName () return $this-$getter(); Call the $getter method. } Else { //Behavior Property $this-ensurebehaviors ();//Call this function otherwise to ensure that this component declares this behavior. foreach($this->_behaviors as $behavior) { if($behavior->cangetproperty ($name) {//Returns a Boolean indicating whether the property is readable. return $behavior-$name; Returns the value of name if it is readable. } } } if(method_exists($this, ' Set '.$name) {//If set. $name This method is called in this object, it returns True. Throw NewInvalidcallexception (' Getting write-only property: '.Get_class($this) . ‘::‘ .$name); Get read-only property}Else { Throw NewUnknownpropertyexception (' Getting unknown property: '.Get_class($this) . ‘::‘ .$name); If not called, throws an exception message: The property is unknown. (not to be continued ...) } }

Yii2 Framework Essay 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.