You are viewing revision #9 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version or see the changes made in this revision.
Understanding Yii 3 ¶
Introduction ¶
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, like the framework being too coupled with jQuery, bower, bootstrap. [TODO: add more grieffs about Yii2]
Changes overview ¶
Here are the main changes in Yii 3. You can check the complete CHANGELOG for an exhaustive list.
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.
Autoloading ¶
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
Application configuration ¶
[TODO]
Dependencies injection ¶
[TODO]
Yii 3 composer packages ¶
Here are the new packages introduced in Yii 3, which can be found in this official list.
Let's introduce them briefly:
The Framework ¶
This is the new kernel of Yii. It defines the base framework and its core features like behaviors, i18n, mail, validation..
You will rarely want to directly install yiisoft/yii-core
. Instead, you will install one or more of the following:
This three packages, considered as Extensions, are responsible for implementing the basic functionnalities of each "channel" they refer to:
yii-console
implements all that you need to build a console application (the base Controller for commands, the Command helper, ..)yii-web
implements all that you need to build a web application (Assets management, Sessions, Request handling ..)yii-rest
implements all that you need to build a REST interface (ActiveController, ..)
Librairies ¶
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.
- yiisoft/log : The logging library
- yiisoft/di : The dependency injection library
- yiisoft/cache : The caching library
- yiisoft/active-record : The Active Record library
- yiisoft/rbac : The role base access control library
- yiisoft/view : The view rendering library
- yiisoft/mutex : The mutex lock implementation
- yiisoft/db : The database library
Drivers for yiisoft/db ¶
The various drivers for DB have also been separated into packages:
- yiisoft/db-mysql MySQL support for Yii
- yiisoft/db-mssql MSSQL support for Yii
- yiisoft/db-pgsql PostgreSQL support for Yii
- yiisoft/db-sqlite SQLite support for Yii
- yiisoft/db-oracle Oracle Database support for
- yiisoft/db-sphinx Sphinx support
- yiisoft/db-redis Redis support
- yiisoft/db-mongodb MongoDB support
- yiisoft/db-elasticsearch Elastic
Extensions ¶
Extensions depends (at least) on yii-core. Aside from the 3 extensions already encountered above (yii-console, yii-web, yii-api), these packages are available
Development ¶
- yiisoft/yii-debug The debug panel
- yiisoft/yii-gii The code generator extension
- yiisoft/yii-dev Tools for framework contributors
View rendering engines ¶
- yiisoft/yii-twig Twig Extension
Data rendering ¶
- yiisoft/yii-dataview : ListView, GridView, DetailView
JS & CSS Frameworks integration ¶
- yiisoft/yii-bootstrap3 : Bootstrap 3 assets & widgets
- yiisoft/yii-bootstrap4 : Bootstrap 4 assets & widgets
- yiisoft/yii-jquery jQuery, ActiveForm
Widgets ¶
- yiisoft/yii-captcha The CAPTCHA Extension
- yiisoft/yii-masked-input : The masked input widget (depends on jquery)
Misc ¶
- yiisoft/yii-swiftmailer Swift Mailer Extension
- yiisoft/yii-http-client HTTP client extension
- yiisoft/yii-auth-client External authentication extension
Yii project template and application bases ¶
This is a very basic Yii project template, that you can use to start your development.
You will probably want to pick one or more of these three starters to install in your project next:
Let's try running the web base template in the next section.
Running your first Yii 3 powered application ¶
Let's try running a web application using Yii 3, and the provided project template.
Installing the project template ¶
composer create-project --prefer-dist --stability=dev yiisoft/yii-project-template myapp
cd myapp
Here's the created structure:
.
├── LICENSE
├── README.md
├── composer.json
├── composer.lock
├── config
│ ├── common.php
│ └── params.php
├── docker-compose.yml
├── hidev.yml
├── public
│ ├── assets
│ ├── favicon.ico
│ ├── index.php
│ └── robots.txt
├── runtime
└── vendor
You won't be able to start the web server right away using ./vendor/bin/yii serve
, as it will complain about not knowing the "app" class.
In fact, this project template only introduce the bare minimum in your application: Caching, Dependencies injection, and Logging. The template doesn't make an assumption about the kind of application you're building (web, cli, api).
You could start from scratch using this bare template, select the extensions & packages you want to use and start developing, or you can pick one of the three starters provided.
Installing the web
starter ¶
Since we're doing a web application, we will need an asset manager. We can pick either one of those:
- Asset-packagist & composer-merge-plugin (requires only PHP)
- Foxy (requires npm or yarn)
Let's go with foxy (personal taste since composer is so slow from Tunisia):
composer require "foxy/foxy:^1.0.0"
We can now install the yii-base-web
starter and run our application:
composer require yiisoft/yii-base-web
vendor/bin/yii serve
By visiting http://localhost:8080/, you should now see something like this:
Checking back our project structure, nothing really changed, aside from the creation of these three entries:
- node_modules/
- package-lock.json
- package.json
So where do what we see in the browser comes from ?
Exploring yiisoft/yii-base-web structure: ¶
If you explore the folder in vendor/yiisoft/yii-base-web
, you will see that the template is in fact a project itself, with this structure:
.
├── LICENSE.md
├── README.md
├── composer.json
├── config
│ ├── common.php
│ ├── console.php
│ ├── env.php
│ ├── messages.php
│ ├── params.php
│ └── web.php
├── phpunit.xml.dist
├── public
│ └── css
│ └── site.css
├── requirements.php
├── runtime
└── src
├── assets
│ └── AppAsset.php
├── commands
│ └── HelloController.php
├── controllers
│ └── SiteController.php
├── forms
│ ├── ContactForm.php
│ └── LoginForm.php
├── mail
│ └── layouts
├── messages
│ ├── et
│ ├── pt-BR
│ ├── ru
│ └── uk
├── models
│ └── User.php
├── views
│ ├── layouts
│ └── site
└── widgets
└── Alert.php
The folders and files should make sense to you if you already developed applications using Yii2 and the basic template.
Good work.
Awesome! Thanks!
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?
src
is 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.
There will be no default JavaScript intergration. Vue, Angular, React etc. are better used through REST.
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.
About the JavaScript integrations i don't think this matter should be left behind to a simple assets management because many people don't love JS(myself included) but we have to use it, and it's like a new tendence to use node js because vue/react/angular default exemples its made with NODE, living the PHP world on some sort of "crisis", i remember a developer telling me that you react it was made for NODE because of this. VUE work better with web services but i can work very well with embebed PHP and a framework implementation to do this.
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
GridView
rendering 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.
regards..
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.
well done!
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?
THanks
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.