4

What should the directory structure of my Yii 2 application be? Does it really makes sense to use Yii 2 views or do I have to create an AngularJS application directory and put the Yii application in one of its folders?

What is the explanation?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ejaz Karim
  • 3,676
  • 6
  • 36
  • 49

1 Answers1

9

I recommend you to split your backend (Yii 2) and frontend (AngularJS) in two separate folders. Dead flies and meatballs should be served separately. Yii 2 just provides the server API, while AngularJS does all other things.

project/
  backend/        // Yii2 app
    web/          // Public visible backend folder
      index.php   // Entry point
    config/
    controllers/
    models/
    ...
  frontend/
    app/          // Your AngularJS application here
      css/        // Styles (.less or .css)
      img/        // Images
      lib/        // Third-party libraries such as jQuery or AngularJS
      js/         // .js files (controllers, services, etc.)
      partials/   // Templates (.html)
      index.html
    tests/        // AngularJS tests
    node_modules/
    ...

The web server should be configured this way:

  • http://mycoolsite.com/api/* requests to project/backend/web/;
  • http://mycoolsite.com/* requests to project/frontend/app/.

If you use Apache as a web server, mod_alias can help you.

Note, that the folder structure inside the backend or frontend directory may vary.

  • For backend it depends on which template you prefer (basic or advanced). In my example I used the basic one.
  • For frontend it depends on your AngularJS application organization. In the example I used the AngularJS Tutorial App, but for huge applications it is better to use a modular structure.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aleksei Akireikin
  • 1,999
  • 17
  • 22
  • If I'm correct in this case I need to put both backend (admin) and API for frontend in backend directory? – Ejaz Karim Mar 02 '15 at 14:05
  • By backend do you mean, for example, the administration login section of a given website, or, even that user interface should be created using Angular? – MEM Jan 26 '16 at 14:27
  • @MEM By backend I mean site API (PHP). The API provides all necessary methods for administrators and other users. As for me, it's better to create admin interface using Angular, but it's possible to use Yii's views instead. – Aleksei Akireikin Jan 26 '16 at 14:42
  • @AlekseiAkireikin Thank you. So, while Yii 2 offer us the advanced template, with views, we create an "advanced API template structure" to first start it. This makes sense I believe. My next project may use both. Out of curiosity, and taking into consideration your structure, your administration dashboards, and admin user interfaces, will have their files under "frontend" yii folder, is this correct? – MEM Jan 27 '16 at 08:41
  • @MEM not really. Usually storing static files (JS, CSS, HTML templates etc.) inside Yii's public folder is a bad idea. Angular apps require NodeJS ecosystem and actually don't depend on Yii 2. In your case, my first level folder structure would be: backend (Yii 2), frontend_public (AngularJS), frontend_admin (AngularJS). – Aleksei Akireikin Jan 27 '16 at 09:38
  • Hm... Angular doesn't require NodeJS. And Frontend/App in Yii, is not a public folder. But I understood what you said I guess. – MEM Jan 27 '16 at 16:04
  • @MEM Very basic AngularJS apps may not require NodeJS. But in reality almost all frontend development requires NodeJS. By public folder I mean web folder of Yii's template https://github.com/yiisoft/yii2-app-basic/tree/master/web - for basic template, or https://github.com/yiisoft/yii2-app-advanced/tree/master/frontend/web - for advanced one. – Aleksei Akireikin Jan 27 '16 at 16:23
  • Yes. You are correct, Web does have js and css exposed. But on the implementation I was suggesting that would never be the case, because, inside frontend/web you will have nothing but and index file. Regarding the NodeJS however, I really where have you read that almost all frontend development required NodeJS when we are talking of complex applications. One is server side, the other is client side, it really is up to you I guess. – MEM Jan 27 '16 at 16:58
  • @MEM I get your point. It makes sense. There's more than one way to skin a cat. – Aleksei Akireikin Jan 27 '16 at 17:31