Understanding Yii 3 ¶
This document is intended for an audience already familiar with Yii2. It's meant to bring together all information related to Yii 3 in one place to make it easier to get on track.
Yii 3 is the second major rewrite of the Yii framework.
Originally started in the 2.1 branch, it was later decided to switch to the 3.X series because of all the backward compatibility breakage. Starting with 3.0, Yii will follow the Sementic Versionning.
This rewrite addresses a lot of issues Yii 2 suffered from:
- The framework being too coupled with jQuery, bower, bootstrap.
- Big inheritance tree.
- Non-standard objects behavior (magic properties).
- Being out of the general PHP infrastructure with tons of extensions wrapping regular PHP code.
- Being monolitic framework where you can not fine-tune by picking exactly what you need.
Source code splitting ¶
The framework source code have been split into several packages, and at its core level, Yii no longer makes assumptions about your development stack, or the features you will be using.
This enable you to cherry pick the packages you need to compose your application.
This re-organisation is also a great news for maintainance, as these packages will be released separately, thus allowing more frequent updates.
The custom PHP class autoloader have been removed in favor of Composer's PSR-4 implementation.
This means that in order for Yii to see your classes, you will have to explicitly register your namespace in
composer.json. We will see an example later.
PSR compatibility ¶
Yii 3 takes some positive steps following the PHP-FIG recommendations, by implementing the following PSRs:
- Logging is now compliant with PSR-3
- Caching is now compliant with PSR-16
- Dependency Injection is now compliant with PSR-11
- and more
Application configuration ¶
If you've ever installed an extension using Yii 2, you may/certainly have found yourself on the extension README file, looking for the chunk of configuration to copy/paste in your own
This can often lead to:
- a huge configuration file (which you may have decided to split into smaller files)
- non-trivials configurations update when a new version of the extension is realeased with new/changed configurations options.
Yii 3 takes another approach. Every package bundle its own configuration, and will probably work out of the box. And you may override them, if you need to, from your configuration file.
This is all done by leveraging the yiisoft/config, which takes care of scanning & merging all the configurations when you run
composer dump-autoload (also know as
Packages authors will have the responsibility to avoid introducing BC breaks, by adopting a strict sementical versionning.
Dependency injection ¶
Dependency injection is a core concept of Yii3. The team intentionally dropped service locator and used injection via type-hinting. You can learn more in the guide.
Yii 3 composer packages ¶
Here are the new packages introduced in Yii 3, which can be found in this official list.
There are two demo apps available:
Guide and other docs ¶
All the current documentation could be found in yiisoft/docs repository.
It seems to me that it is better to rename src to app
"If you explore the folder in vendor/yiisoft/yii-base-web..."
Зашел, увидел - код то свой где писать?
В папке vendor/yiisoft/yii-base-web ???
Walked in and saw code that your where to write?
In the vendor/yiisoft/yii-base-web ???
What if i want to have in my app web and console?
What do we have an src folder? can't we have all those folder in the root? and what about the modules?, but what ever choise you make, please please please do not rename the folder src to app because that will be a lot like laravel, just please don't do it ;).
I Hope Qiang Xue its involved in this because his choises of in Yii 1 and 2 of how a framework should be its the reason i choose Yii over the others.
The separation of web, console and rest sounds very nice thanks for that one.
Another thing, can we have some kind of integration with VUE JS over jQuery for the templates created by gii? I mean, jQuery deserves our respect but its getting old and we need better ways to integrate the new stuff like Vue+React+Angular.
Thanks a lot for the job, can't wait to have the first alfa to test..
There is something I do not really understand:
In Yii 3, libraries do not depend on Yii and are meant to be usable outside the framework. Their package name is yiisoft/something without yii-prefix.
Sounds good. But looking into the source of yiisoft/db I see, that it referes to base/Component which is implemented in yiisoft/yii-core.
Are there plans to move the very basic classes/interfaces to a yiisoft/yii-base? I would welcome that as I had cases before where I wanted just to use Yii's Base-Classes. And in this case the description that libraries do not rely on the yii framework would make more sense. Or are the yiisoft/yii-web (etc.) meant to be the framework implementation?
srcis a convention that is used in majority of modern PHP packages out there. We find it OK and don't want to be different just to be different.
Require two packages from a template: both web and console.
Qiang isn't participating in Yii since summer 2015. We are doing everything according to Yii values.
Package names show intent. Not all packages are currently dependency-free. The ones with
yii-prefix are meant to be dependent on Yii-specific packages.
First things first, thanks samdark for taking the time to respond to our comments.
So like i said before i'm not a big fan of frontend development but having to integrate Yii with any of those js frameworks leaves us with a competitive disadvantage, maybe this integrations do not deserve to be core but i can be optional like the Bootstrap and jQuery UI integrations, and i really will love if our webapps made with Yii3 worked like a SPA in the default GridView and stuff like that and VUEJS can be a very good choise for this, i mean in Yii2 by default we have jQuery, why can't we have now VUE JS by default?.
Finnaly the other topic are web sockets, i think we need this too in order to make modern apps, this is a must have in nowdays and PHP can work very well with this too but normally we don't have it and we have to make it from scratch. If Yii3 had all this i can be by far the best PHP framework without question because this is needed in the PHP World.
Sorry for making this so long.
Apologies everyone, it seems like I wasn't subscribed to comments. I just discovered all the conversation :-O
In this new tendency you're describing (JS Apps), PHP have still a great deal to offer by acting as a backend/API server.
The frontend is made using Vue/React/Angular and uses
fetch()to target an API server.
Having written some apps using these technologies, after several years of generating HTML with PHP frameworks, I must say that it's a delight to work with this new stuff. For the first time I felt like my apps were "alive". But that's not our topic of conversation ^^
Here, you're not talking about Node based apps anymore, but PHP based ones. Which is another, and totally valid but older approach.
This is what Yii2 & jQuery were all about. But Yii 2 was too tightly coupled to jQuery and Yii 3 wants/needs to be Framework agnostic.
Having a Vue implementation as a "default" library would be a bad thing in my opinion, because some people prefer React. Or jQuery for that matter.
For the time being, Yii 3 packages are being rewritten to use vanilla JS (= no lib). And that's just great.
You mentionned how
GridViewrendering could be super using VueJs, and I totally agree with you. But I don't think the Yii Team would have the time to maintain a widget like this.
The focus is instead on freeing developpers from whatever technologic-chain they had until now, to let them do their own GridView & ActiveForms validations, Widgets using their favorite JS framework.
I'm confident the community will be quickly offering integrations with various frameworks, and I'm eager to see the results :)
Like Mehdi Achour sais using PHP based apps it's the old way, my point is that i want to use PHP and also feel my apps "alive" as well by integrating the new stuff into it, if we don't have this the new Yii3 will be to do stuff the old way or only to make REST APIs. We wont be able to use Yii's widgets to make modern web.
Honestly i don't know how to do it without choosing a JS framework, it's like if now JS it's more important than ever and if we want to be up to date with web development we need to use those things.
if you choose to use vue.js or react.js, Yii only need to provides REST api, this simplifies the development process, this is a trend, this increases productivity.
I think the yii development team needs to focus on PHP, let the community offering integrations with various js frameworks
Server development has now come to a new era, PHP is rarely used in distributed system developement and real-time system development, PHP needs innovation and evolution
I agree with Meng Yeg that innovation and evolution it's needed but i don't think that should be a PHP's alone task, look at JS, the evolution it's because of the implementations and uses of the language not the language itself and we know PHP can do all of those perfectly and can be even stronger in other things.. We need tools that allow us to use WebSockets more easily, we need widgets that can work with the regular HTTP calls and with ajax calls from the box. A very valid solution it's to make this kind of things with extentions and not the core of the framework, but we have to keep in mind that all this stuff is needed like we need to connect to a database of have clean URLs and things like that, this is a must have in this days.
I'm a long time user of Yii and I love the framework. But I do agree with others that it is very important to recognize the outsized role that React and it's ilk are having on both front end and backend (because providing JSON to the front end is now absolutely a neccessity). I wish it were the case that Yii's REST pattern was the best way to do this, but REST is clunky compared to websockets and Pub/Sub messaging systems.
The way I have handled this in my own projects has been to create a Node.js server that speaks these protocols to the UI and interrogates Yii APIs through the REST layer within localhost. This is not ideal, although it is faster in some circumstances. If there were a websocket-speaking adapter in Yii 3 that would allow direct interaction from the UI, that would be ideal, but obviously this is not the usual lifecycle of a PHP application.
Still, as others have said, if this could be solved it would be a tremendous advance.
When will it be officially released?
When it's ready to be released.
Great job Mr. @samdark, Many many congratulation on this new release. Hope community will enjoy,utilize well as Yii2 and support.
Added dependencies graphs for all packages at https://yii3.idk.tn/data-view/list-view
Great job Mr. @samdark, I've been using yii for past 2 years, it was amazing but now it's going to be perfect. Thumbs up to the YII team. How can I contribute in any possible way?
Thanks again for your efforts, you're doing an amazing job.
Well, great work guys ! Can't wait to work with it
good news, i think yii3 should use HMVHMVCHMVHMVCCHMVHMVCHMV instead of MVC to put each module into a separated directory (so it will easy to add/move a module) (each module have its own controllers, models and views)
HMVC instead of MVC (this textarea is very buggy on google chrome Android)
Instead of src we could use protected/app it is much familiar.
In Yii2 we have templates: basic and advanced. I'm interested in Advanced template because my enterprise projects use backend/frintend architecture. How to implement "Advanced" template with Yii 3?
eshi, there's going to be a single template for Yii 3. It's not ready yet but you can check yii-demo package.
Concerning the folder structure I would say that at least "config" and "public" is part of your source for the application - so it really doesn't make sense to me to have them outside the "src" folder...?!
Also, navigating in and out of the src folder will be an extra hassle when coding... (Side note: If you just use Total Commander it's super easy to filter to the folder you want and hit Enter to go to it! But not with the extra level src causes...)
I've been working with Yii1 and Yii2 since 2013, and I really love this framework.
Now I'm really concerned with the future of this framework.
I'd like to suggest Swagger as a base feature for the Yii 3. I tried to use it in this project https://github.com/yii2-starter-kit/yii2-starter-kit, but it is not updated and had no success trying to update that.
Another point is that I love Gii module, the way we can create an entiry CRUD... Hope some day we can make a frontend CRUD, just choosing the frontend framework, like React, Vue, Angular or even NextJs. I think that would be a very good evolution for we to stop using php views, and make it just using rest APIs, or with sockets.
Maybe I'm dreaming with a new frontend & backend framework, with the Gii module for frontend frameworks, based on MySql/MongoDb/Firebase databases, and the Next.js features for deploy previews, development and production environments easily, and as fast as localhost, cached APIs, CDN, all included ...
I'm from Brazil, so I really would like to contribute, hope doing this soon.
@georgezim85 @samdark Swagger as a base feature for the Yii 3，It's a great idea
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.