I want to run some code in the onBeginRequest event.
Where do I do that? I assume I am not suppose to add this in the core library code.
I am a totally noob in Yii
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I believe you can do this pretty much anywhere in your files before any output has started, so it should work in a controller, view or custom class, usually located in the "protected" folder in a Yii web app. FYI, those files are not core files and can be (nearly) freely edited, as apposed to the Yii framework files (as referenced by the "$yii" var in the bootstrap index.php file).
the functions look like:
If you want to use onBeginRequest and onEndRequest you can do it by adding the next lines into your config file:
or you can do it inline
where
Y
is a classname andgetStats
andwriteStats
are methods of this class. Now imagine you have a classY
declared like this:So on every request both methods will run automatically. Of course you can think "why not simply overload onBeginRequest method?" but first of all events allow you to not extend class to run some repeated code and also they allow you to execute different methods of different classes declared in different places. So you can add
at any other part of your application along with previous event handlers and you will get run both
Y->writeStats
andYClass->someMethod
after request processing. This with behaviors allows you create extension components of almost any complexity without changing source code and without extension of base classes of Yii.