yiiframework.com | Source code for official Yii website | Web Framework library
kandi X-RAY | yiiframework.com Summary
kandi X-RAY | yiiframework.com Summary
This project contains the source code for the yiiframework.com Website. If you want to contribute please get in touch with us using the issue tracker.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Generates a guide
- Renders the index ranking .
- Import users .
- Set mappings .
- Get the raw data .
- Lists an extension .
- Load extension data from Packagist API .
- Renders a list item .
- Searches for news type .
- Handles user registration .
yiiframework.com Key Features
yiiframework.com Examples and Code Snippets
Community Discussions
Trending Discussions on yiiframework.com
QUESTION
I'm new to Yii2 and returning to PHP dev after a very long time. I have an extensive background in Java development. I stumbled onto this recommendation in the docs for ActiveRecord:
__construct()
public methodDefined in: yii\base\BaseObject::__construct()
Constructor.
The default implementation does two things:
- Initializes the object with the given configuration
$config
.- Call init().
If this method is overridden in a child class, it is recommended that
- the last parameter of the constructor is a configuration array, like
$config
here.- call the parent implementation at the end of the constructor.
My question is about the last sentence:
call the parent implementation at the end of the constructor.
As a Java dev, this advice seems very weird to me. In Java, not only is it recommended to call the parent constructor as the very first call from your overridden constructor, this is even enforced and it's actually impossible to do it any other way. Either your parent constructor is called first implicitly, or, if you make an explicit call, it MUST be the very first statement in your method. This is enforced by the compiler.
Theoretically, this makes a lot of sense to me. Because as long as you did not call the parent constructor, your parent class did not have the chance to initialize, so any code you write in the constructor before calling the parent constructor would be working with a half-initialized object.
Looking at some SO answers I found, they seem to be going against the advice in the official docs and call the parent::__construct
before their own custom logic, as I would expect it. For example, the accepted answer in the question How can I create a constructor in a Yii2 model shows an example where they call the parent first:
ANSWER
Answered 2022-Apr-02 at 06:21@michal-hynčica correctly stated the reason for your ambiguity(Worthy of votUp).
But since you mentioned that the official reference, on the same page (BaseObject), this issue is explicitly stated: line
- In order to ensure the above life cycles, if a child class of BaseObject needs to override the constructor,
- ....
- of the constructor, and the parent implementation should be called at the end of the constructor.
According to the description of this link (similar to your question):
init () is called which further calls bootstrap () to run bootstrapping components.
As a result, this is done to ensure configuration.
The same process is done in the controller class and the main module.
Of course, there are many examples like this on the php site (depending on the need)
Also read lifecycles and entry script in yii. components and structure-applications. Good luck
QUESTION
Yii2 provides an elastic search extension and also elastic search ActiveRecord class.
This elastic search extension seams to read the data from elastic by querying the _source
field of elastic search, which is not ideal.
If fields in elastic are set to "store" :true
it would be the better way to read the data from the stored field directly, not by querying the _source
field which results in an implicit parsing of that field by elastic.
Is there a way to configure or use Yii2 / elaticsearch - extension to get the data from the stored fields? I didn't found any option or something else here: https://www.yiiframework.com/extension/yiisoft/yii2-elasticsearch/doc/api/2.1/yii-elasticsearch-activerecord
...ANSWER
Answered 2022-Jan-24 at 09:12To answer my own question in case someone else has the same problem:
If you like to run an query using active record in Yii2 on stored fields and if you like to get the data from the stored entry, not by parsing the _source
filed, than you have th explicity specify the stored fields by using the ->storedFields()
- function from active record class.
Using active record it looks like this:
QUESTION
I am currently working on a program where a user can change his profile data, such as name and password ect. However, I want to limit the user to change the id in the url and then be able to look up some other users data.
For example, when user with the ID 1 wants to change his password, he uses the following url:
http://localhost:8093/myapplication/web/index.php?r=user%2Fupdate&id=1
Now I want to prevent user 1 to simply change the URL to
http://localhost:8093/myapplication/web/index.php?r=user%2Fupdate&id=2
and be able to manipulate the user with the said ID unless he is a admin of course. I have been using access rules to limit the access of non admin users to certain actions, however the non admin user must be able to change his own data as well.
This is what I have in the UserController.php
:
ANSWER
Answered 2022-Jan-17 at 07:08You could check if the logged in person has that ID, if yes they can do it and if not, error.
QUESTION
In Yii doc, it is said that
Besides controllers, you can also declare filters in a module or application.
But, how to implement that in application, programmatically?
...ANSWER
Answered 2021-Jul-02 at 16:43You can attach behavior for application in its config.
For example attaching ContentNegotiator
in your frontend\config\main.php
:
QUESTION
The official Yii docs talk about adding rate limiting to an api by implementing yii\filters\RateLimitInterface on a user identity class.
https://www.yiiframework.com/doc/guide/2.0/en/rest-rate-limiting
But is it possible to implement rate limiting on classes that aren't the user class?
For example in my api a User belongs to an Account. An Account has many Users.
Is it possible to implement rate limiting per Account, rather than per User? If so how?
...ANSWER
Answered 2021-Jun-24 at 09:20You have two options how to do that.
First option is to implement RateLimitInterface
in same class that implements IdentityInterface
but load/store allowance in your account model.
If your User
model implements IdentityInterface
and has Account
relation it can look like this:
QUESTION
I'm using yii2 framework to make a website do upload to Cloudinary through its API. but for some reason the API function that requires a path of the file to do the upload.
files in web folder can be uploaded but I don't want to make upload to my server and than send it to cloudinary server (too many actions), I want to make upload from my local host directly to cloudinary server
the upload code from yii2 controller:
...ANSWER
Answered 2021-Jun-07 at 22:26You should pass the file path you are passing the UploadedFiles
object which is unknown to the Cloudinary upload api.
You should pass the path via $image->tempName
to the (new UploadApi())->upload()
like below
QUESTION
I'm using the Nav widget in Yii2. I have a dropdown menu as part of the nav menu but the dropdown is very long and extends beyond the bottom of the page and doesn't scroll. To solve this I am trying to add the pre-scrollable
class to the submenu container. Try as I might I can't seem to make it work.
In the Yii manual for the Nav widget (https://www.yiiframework.com/extension/yiisoft/yii2-bootstrap/doc/api/2.0/yii-bootstrap-nav) under the $Items
public property it says:
dropDownOptions: array, optional, the HTML options that will passed to the yii\bootstrap\Dropdown widget.
I've also looked at the man page for the Dropdown widget (https://www.yiiframework.com/extension/yiisoft/yii2-bootstrap/doc/api/2.0/yii-bootstrap-dropdown).
The code I have for my Nav widget is like:
...ANSWER
Answered 2021-Feb-24 at 00:56The option dropDownOptions
is tested successfully on Yii version 2.0.11.2 and Bootstrap v3.3.7:
QUESTION
I'm trying to use the Architect Free Template in a yii2 basic Application.
I have an issue when I use the tags
...ANSWER
Answered 2020-Dec-17 at 22:37Sorry for bother i found why its failing, as @ustmaestro said, yii2 has a different bootstrap version and the template uses bootstrap4.
After install the yii2 bootstrap4 package it works perfectly.
have to change the code to insert bootstrap4 functions.
Greetings.
QUESTION
I have reinstalled my Yii2 application using newest server stack from Bitnami. But there is one remaining issue that I cannot resolve in any way.
Mpdf does not see my custom fonts despite having defined this before Pdf instantiation:
define("_MPDF_SYSTEM_TTFONTS", Yii::getAlias('@webroot/fonts/'));
I am getting this error:
...ANSWER
Answered 2020-Nov-30 at 10:17Just try to import your fonts in simple template file, if it's working fine, the problem will be with your path aliases. (I mean for example with @web or in your case with @webroot)
Or you can use Application Assets, you can write your own and you can define all dependencies what you want to use for the application. Also there are some examples with aliases and you can try out what will be happen if you use them like there.
QUESTION
How do we attach a behavior to all web controllers in the application?
I understand this is theoretically possible with dependency injection, so I assumed something like this would work:
...ANSWER
Answered 2020-Nov-12 at 10:02Interesting problem, I must say. I could not find the simple solution for this but I have this hacky idea. You could take advantage of Yii autoloader and load your version of yii\web\Controller
instead of the original one.
To do that:
Copy the original file from vendor and place it in your app
Don't change the original namespace and name.
Add your behavior config (or just the behavior's code, whatever) inside.
Add this line below in a place that will be called every time the app runs (like entry point file or bootstrap file, it must be called after
vendor/yiisoft/yii2/Yii.php
file isrequired
):
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install yiiframework.com
During development, run gulp to watch view, Sass and JS file changes and automatically build target CSS/JS files. This command will also launch a browser window which is connected to browsersync.
At any time, run gulp build to manually rebuild target CSS/JS files from source Sass/JS files.
If you only want to watch for changes, you can issue the command gulp watch
To build the assets for production, specify the production flag: gulp build --production or run npm run build
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page