9

I have almost completed a PHP project, using MVC, jQuery and Ajax. It is pure PHP project. I don't use any frameworks in the code right know. I would like to change that. Doing some research, I found, that Yii turns out to be one of the best frameworks out there.

Is it possible to somehow migrate pure PHP project to Yii?

If so, then how to do this? Which steps should I follow in order to reduce the workload and enjoy the benefits the Yii framework presents?

I'm a total Yii newbie, any insights appreciated.

trejder
  • 17,148
  • 27
  • 124
  • 216
Pedro Herencia
  • 129
  • 1
  • 4

5 Answers5

48

TL;DR : Don't do it. It's a really horrible idea.


The Rant ..

"Framework" is not a magic sauce, that you add to a project, to make it better and shinier.

Doing some research i found Yii turns out to be one of the best frameworks out there.

What a strange research you have done .. I would love to see the materials. Especially, since I would rank it as 3rd worst PHP framework. Only surpassed in it's awfulness by CodeIgniter and CakePHP.

And the reason for it is the extremely bad quality of code, that this framework displays, combined with the bad practices, that it perpetuates.

Why avoid migration?

From your description is obvious, that you are NOT familiar with this framework and have no previous experience with it.

In management of projects there a subject: risk management. And in this case, adding a previously unused framework in final stages of project would constitute a high probability an high impact risk which also, due to the sage of project, is completely unmitigated.

It means that there is a really good chance that something will go wrong. And when it does, it most likely will sink the project. Or at least push back the release data by significant length of time.

In a perfect world frameworks are used to simplify the repetitive tasks of development, at the cost of some performance. Those are the tasks you do at the start of the project. You are not at the start of a project. This means that you will gain no benefits from this "maneuver".

Why not Yii?

As I noted before, there are also reasons not only for avoiding adding framework to an existing project, but also reasons why to avoid Yii in particular.

The inheritance nightmare

All your controller will extend class CController, which extends CBaseController, which extends CComponent

All your "models" will extend ether CActiveRecord or CFormModel, which extends CModel, which extends CComponent.

Both of there chains contain static variables and execute static methods on multitude of different other classes. Combination of these factors will make debugging extremely difficult and tedious.

Global state

There are several forms of global state. One that people in PHP usually know are global variables. But that is not the only form. Every time you have a class that contains a static variable, it also creates a global state, that can (and almost always - will) cause seemingly unrelated instance mysteriously interact.

Use of global state is a core mechanic. You will see static calls all over the codebase, and the Yii's configuration file would not function without global state.

And every time you call Yii::app() you are accessing and/or changing it.

This makes unittesting impossible for Yii applications. And debugging turns into exercise of using grep on your whole project.

Tight coupling

When you create an application in Yii. It becomes bound to it. You cannot execute parts of your application without launching the full framework. Mostly it is due to the static call, that you end up adding to your code.

Every time you add a static call in your own code, that piece of code becomes tied to the name of the class. That essentially is tight coupling.

As you might have noticed (hopefully), there is another way how to achieve the same effect - the use of new operator. That is another way of coupling some code of yours to a specific name of a class.

No interfaces .. none .. whatsoever

No matter how horrible the configuration of a Yii project is, the configuration file was a well intended gesture. The least harmful way to introduce external code and replace existing components in so messed up codebase.

But unfortunately it brings in the focus the problems caused by lack of interfaces and the existing coupling.

One of the component that developers will try to replace is the CUrlManager. Mostly due to way how you can pass additional parameters.

An interface in OOP specifies the contract between two instances. It lets you define the capabilities of an instance, the methods that can be used by others. When it's not there, in a large codebase, you are left guessing, which methods are required and which are not.

In case of Yii components the problem is compounded even further due to static call and deep inheritance. The above mentioned CUrlManager extends CApplicationComponent, which extends CComponent. Also the same file defines CUrlRule and CBaseUrlRule classes.

When you are writing a replacement, you have to write some code, plug it in the configuration and then test it by running your applications. That way you know which method (or parameter) next you need to add.

Basically, it's the "save-an-see-what-blows-up" method of development.

That's not MVC!

Yii does not implement MVC or any of MVC-inspired design patterns. What it calls "MVC" could be described as ActiveRecord-Template-Logic pattern.

Instead of having proper model layer (yes, it should be a layer), the creator(s) of Yii opted for collection of active record and form wrappers. This forces the application logic to be forced in the "controllers".

On the other hand you have glorified templates, instead of proper view instances for containing presentation logic. It is somewhat mitigated by use of widgets, but those instead suffer from SRP violations, because widgets are forced to contain bits of presentation logic and perform partial rendering. The rest of presentation logic ends up again in the controllers.

And to make it all worse, the "controllers" also have to deal with authorization. This usually will mean, that whenever you change the access scheme, you will have to go through every single instance of CController to check whether it needs to be changed too.

It's not MVC. It's a mess with names taken from MVC design pattern and slapped on some components.

All the small things ..

The framework also comes with few minor issue, that do not deserve a separate section:

  • Defining more then one class per file:

    This will get annoying quite fast, because there will be classes that are shoehorned at the class files with completely unrelated filenames. This will mean, that debugging will quite often require use of search.

  • Slapped on "modules":

    By the looks of it, the modules where added to the framework after the fact. Which is why, when you need to set default module, you will have to set it in the configuration files parameter, that is called 'defaultController'.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 4
    Very informative. Is there any PHP framework that you'd recommend ? – tacone Jun 13 '12 at 19:47
  • 6
    @tacone , maybe in some cases Symfony2/Silex (though they have DIC as core structure, which i would consider an antipattern), but it all depends on specific requirements. Currently all PHP frameworks suck. The trick is to find one which for your exact situation would be the lesser evil. If you even need a framework, that is. – tereško Jun 13 '12 at 20:31
  • 12
    @tereško I don't agree with you. Yii is a MVC framework and one of the best, and it's not an OOP Standard APP, which clearly would be hard to code and no-one will ever choose such. Yii is a rapid application development framework, and that's it. – Pentium10 Jun 27 '12 at 08:05
  • 22
    @Pentium10, in MVC the view in not a template, the model is not an ORM, and the controller is not responsible for passing data from model to view. **It is not MVC.** And how can you say that it is "one of the best", when the codebase practically runs on global state and tight coupling. Have you ever tried to replace a core class in Yii ?! It is a nightmare. And what is "OOP Standard APP" ?! – tereško Jun 27 '12 at 08:15
  • 3
    Yeah I tried to replace a core class in Yii, there is an array where you can define your own overrides. – Pentium10 Jun 27 '12 at 08:45
  • 3
    @tereško, please, could you explain yourself better and give us some real examples of "global state", "tight coupling", "view is template" and "not unit-test to check"? I would be really grateful if you could do it cause i love improve my knowledge about design principles (GRASP). Thank you. – sdlins Aug 17 '13 at 20:39
  • 5
    @sidtj , made some updates, though, f you want to research OOP and associated practices and principles, I would suggest you go through [this list](http://stackoverflow.com/a/16356866/727208). – tereško Aug 17 '13 at 23:53
  • 3
    @tereško I would have to disagree on some issues as well. Yes in the strictest sense it is not a MVC. Although for web sites how do you create a framework that is strictly MVC. Also the notion that MVC solves all your problems is not correct either. One major advantage you are forgetting it is superfast and in my experience having some sort of structure is better than no structure. I would say Symphony2 is the biggest competitor but that has issues as well. So the best solution is use what works best in your situation. – Atherion Jan 14 '14 at 18:35
  • 3
    @Tixel what exactly are you disagreeing about? I mentioned MVC **only** because Yii names it as its major feature .. since its *interpretation* of MVC actually is "activeRecord-logic-template" (which breaks SoC twice) , it seems that they are lying in their marketing (oh .. watta surprise). And where exactly did I say that MVC solves all the problems? And I wonder, how did you come to the conclusion that it is "superfast"? – tereško Jan 14 '14 at 18:42
  • 3
    @tereško Ok when has marketing ever told the true story, anyway it is not a valid reason not to use something anyway. Compared to enterprise frameworks like Symfony2 and Zend that rely heavily on caching, Yii out performs them. I am not saying it is the best but definitely a contender. So your giant rant of the issues, what are the alternatives? Raw php has way more issues and you are probably doing it wrong. With all that in mind with frameworks, PHP inherently has issues itself. I do agree on some issues that you point out, but there are features that make it good as well. – Atherion Jan 14 '14 at 19:33
  • 2
    The inheritance nightmare making debugging extremely difficult and tedious isn't true. It's much easier to debug Yii code than code that's more "correct", patternized and layered too much. – Sam Dark Jun 18 '14 at 08:04
  • 3
    The fact that unit testing Yii app isn't possible isn't tru as well. Yii projects are perfectly debuggable. There are tests for the framework itself: https://github.com/yiisoft/yii/tree/master/tests and you can write unit tests for your app: http://www.yiiframework.com/doc/guide/1.1/en/test.overview. Yes, it's not easy to write ideal tests for just a single component at a time but in a real practice it's rarely the case. – Sam Dark Jun 18 '14 at 08:05
  • Tight coupling isn't too bad if you're not going to take parts of your controller code and reuse these in different project with different framework that is unlikely to happen. – Sam Dark Jun 18 '14 at 08:05
  • 1
    Having proper model layer is entirely up to developer. I have no problem keeping my business logic in a proper domain layer when using Yii. – Sam Dark Jun 18 '14 at 08:06
  • 2
    Some of your points are correct though. That's why there's now Yii 2.0: https://github.com/yiisoft/yii2 – Sam Dark Jun 18 '14 at 08:06
  • 3
    @SamDark The fact is that Qiang Xue **has no clue** about SOLID principles and OOP patterns is quite obvious when looking around Yii's code base. Qiang Xue should better learn how to write good and clean code before he starts claiming that it's best framework for web-application. Yii (even 2nd version) is full of anti-patterns and bad practices. One common example : When you call `Yii::app()->run()` you're accessing a Front Controller. When you call `Yii:app()->db` you're accessing a Service Locator. Have you ever heard what a **god object** is? That's it `Yii::app()` returns a god object. – Yang Jun 19 '14 at 04:33
  • 4
    Believe me, all the core team including Qiang are well aware about SOLID and patterns. We aren't fanatics, we aren't trying to make everything theoretically "correct" but instead keeping a good balance between API simplicity, performance and formal correctness. – Sam Dark Jun 19 '14 at 13:10
  • 2
    Symfony deliberately avoids calling itself an "MVC" framework. Apart from the fact that MVC isn't possible in a standard request -> response context (no, that's not being anal, it's a *fact*), deliberately missing out this in the framework docs shows you know what you are talking about and want to avoid that minefield. As for "nobody using a framework that adheres completely to OOP-only principles" - that's just false. – Jimbo Jun 19 '14 at 13:26
  • @Jimbo umm... who said "nobody using a framework that adheres completely to OOP-only principles"? – Sam Dark Jun 19 '14 at 23:49
  • 1
    @Scorpion what you're calling a model is domain model. What you see typically called model in Yii is data model. Yes, one can mix these two but one can also use these separately. Yii has many antipatterns but that doesn't mean that's not intentional and that it actually hurts development speed or execution performance. It's the other way around. – Sam Dark Jun 19 '14 at 23:54
  • 2
    Lots of Yii users indeed think that many other frameworks are unnecessary complex and "academic". There's nothing about "only very-skilled devs can only use that" but entry level of "academic" stuff is definitely higher. – Sam Dark Jun 19 '14 at 23:56
  • 2
    @SamDark What Qiang Xue calls "model" is Anemic Model (http://www.martinfowler.com/bliki/AnemicDomainModel.html). And there's no such thing as `Data Model` in OOP. And how to use validation rules and database abstraction separately in Yii since there's no `Services` and `Data Mappers`? – Yang Jun 20 '14 at 01:28
  • 1
    Really, on the internet you often see some posts of Yii core team members. They will make you laugh. It is funny how people are rating Yii as a good or the best framework, while really it teaches you very bad practises. See this for example: http://www.slideshare.net/samdark/yii-frameworks-and-where-php-is-heading-to – Steffen Brem Jun 28 '14 at 22:19
  • 3
    Yeah, @SteffenBrem, that pack of slides was just horrible. – tereško Jun 28 '14 at 22:35
  • 2
    @tereško it seems like you're exaggerating quite a bit. It's very easy to replace core classes, just extend them and override them in the config. Also, I don't see the fuss about the god object. You see Yii::app(), and you immediately throw out a knee-jerk response, like a C# programmer seeing a goto. It has its uses. It's not inherently bad, just because some programmers misuse it. It's better than polluting the global namespace. Also, there simply is no "true" MVC in a web-based language. –  Jul 10 '14 at 11:19
  • 2
    @Rokk He's not exaggerating at all. What you call `easy-to-replace` is a typical way of breaking **Open-Closed Principle** which Qiang Xue encourages you to do. What is a god object by definition? It's an object that does knows and does too much. First of all `Yii::app()->run` has a `Front Controller`, then since you can also access `Yii::app()->db` it has a `Registry/Service Locator` implementation. (btw, which breaks SoC and SRP). That's why `Yii:app()` returns a god object – Yang Jul 11 '14 at 17:44
  • 4
    @Rokk , when I write about to "hard to replace core classes" I mean the fact that there are no interfaces, all the variables are public and half of the methods are static. After you override them in a global config, you have to run everything just to see which of those public variables and static methods are used somewhere. There is no way to know beforehand, what you need to have in a class for it to be a replacement. – tereško Jul 11 '14 at 18:11
4

I actually recently converted a MVC pattern website I had built from the ground up into Yii. It did take some time to set it all up but in all honesty it was well worth it. I was able to throw away a lot of code because there were already Yii extensions doing what I needed. I would also suggest that you keep your database because you can create the controllers and Models using Gii which will save you a ton of time.

Jacob Waller
  • 4,119
  • 3
  • 26
  • 32
3

I don't know of any quick solutions to this. It depends upon how the code was written. You have the database and your views so it is not really a complete new project when you take into yii. Yii will generate the database models for you. You already have the views from the existing project. Write the controller and actions and modify the views if necessary.

try these links as they refer to the same problem.

How do you convert an old oop project into Yii

tips on migrating an existing site to Yii

Drupal to Yii Migration

Community
  • 1
  • 1
Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • depends. if you have many crud operation forms you will have rewrite them into CActiveforms to take advantage of the validation – Narretz Jun 06 '12 at 18:10
0

Since you already have a code in mvc, things will be much easier for you to migrate. However, while migrating to Yii, since it can generate controller and model very easily using gii, you can take the advantage of it.

So, first generate controller and model using gii, then you can replace your existing code (by replace I mean, substitute your code to the specific function in the controller and model) to the built in controller and model so that the functionality of your site still works. You can modify your view accordingly. But that won't be much of a work.

You can simply register your script for ajax, jquery and css. Those will work as well.

And yes, Yii is the best framework out there so take as much benefit as you can.

Thanks, Ujjwal

Ujjwal Prajapati
  • 286
  • 1
  • 5
  • 14
  • 3
    Yeah, "Yii is the best framework". Tell you what, you come over to the UK or US and talk to software engineers that *know* what they're talking about.. if you say that, it's not going to go down well. I suggest you start learning why your framework of choice is poor. – Jimbo Jul 03 '14 at 08:53
  • 1
    well.. I was just presenting my perspective on Yii. Saying me It is the best out there won't matter much as I am nobody and in no place to say that. To me, it is of course best because that's what I work in :) – Ujjwal Prajapati Aug 12 '14 at 14:03
-1

In this project you converted php to yii framework. Its really easy for you if you do following step.

Since you already have a code in mvc, things will be much easier for you to migrate. However, while migrating to Yii, since it can generate controller and model very easily using gii, you can take the advantage of it.

second, If your database is accurate then 50% work complete.when you create CRUD operation using gii then automatically model-view-controller create.if you create mvc in php then it benifit for you.

third,You can simply include your script for ajax, jquery and css. Those will work as well you create a folder in themes(CSS,JS,AZAX,BOOTSTRAP).

four-Protected->view->layout, where you can change your theme..thats all

you also help www.yiiframework.com/doc-2.0/guide-intro-yii.html

if you think my answer is help you then rating me...thank you.

Santu Das
  • 1
  • 1