Where is the ServiceLayer in YII ?

Where is the ServiceLayer in YII ? And how to implement it ?

Sorry do not mean to compare, but I think as a Best for Web 2.0 Framework should follow the best practices and the design pattern

The Service Layer in Web Application:


Also sorry me, but Yii is not Grails. Also I never heard about “the service layer” as a “best practice”. I suggest you to read The Guide and learn Yii’s philosophy and patterns, because Yii is not a copy of any other (including Java) framework.

1 Like

This is my personal feeling. I can’t speak for the creators of Yii, because I’m not a part of dev team. But in the document you cited there is a sentence saying: “The Grails team discourages the embedding of core application logic inside controllers”. If I’m not mistaken The Yii Team does something exactly opposite, it encourages embedding of core application logic inside controllers. And probably this is why you can’t find service layer in Yii.

BTW: In the demo application, generated by yiic webapp command, you may see that some basic operations, methods and variables, reusable by all or most of controllers are separated into application component - file Controller.php places in components folder. For me this is a quite nice example how common elements are separated from controllers, making them reusable. Can you call this service layer?

You, know… :] No one is forcing you to use Yii, after all! :] If you want to develop your application with using service layer, then you should go to Grails! :]

I would recommend fat models and thin controllers. In particular, to avoid building sql or active record criterias in the controllers if possible.

Hi Wei, thanks for the clue. But then again, it just felt like a missing pieces. Fat models and thin controllers is what we did here, als, separating actions into their own classes and tiny pieces.

But, still it’s just felt like a missing piece.Please don’t get me wrong, I love Yii so much. Yii creators have done a great job! It’s a programming with art!

You are not that friendly, aren’t ya ?

If three smiles in one, rather short sentence are not enough proof of me being friendly, then you are right - I’m not! :]

Don’t take it too personally. If I was too rude, then sorry! :]


In Yii service layer is up to you. You can create it and Yii will not be an obstacle but also you can omit it if you are feeling comfortable without it.

I’m not familiar with Grails but, as I can see from docs, services are just classes used as singletons. So Yii components should be very close.

Yii has not service layer by default but we can obtain by creating services folder next to models and put service classes into it. The services will use models to do business logic. In controllers, we use these services instead of using model directly.

Later, we can re-use these services to expose external APIs or webservice.