0

I use to build my web projects with CodeIgniter before I learned Angular. The MVC structure of CodeIgniter made organizing my php files easy. I no longer had php scripts scattered all over the place. Now that I've learned Angular, it seems that the whole scattered script thing is coming back because angular is front-end stuff and a lot of http requests (to php) files need to be made . I now usually have files like these

login.php in api/accounts and get-likes.php in api/post-actions

They are just single files that do one thing. The structure that MVC frameworks provide is more scalable, almost everything runs through classes, there is one entry point and controllers a automatcially called from urls. Is this wrong. Is there anything I can do better? (Note not on angular's end now but on organizing my script on server).

I know my question is somewhat disorganized and unclear I can feel it myself but I don't know how else to ask. If any edit needs to be made, I'll gladly make them.

2 Answers 2

1

You don't need to go back to the old php ways. Your controllers can just be REST controllers, which return data in JSON format.

For instance, you can have a Authentication controller, which handles all requests regarding authentication.

/api/authentication/login (POST)
/api/authentication/logout (GET)

In that, you can use any framework in your back end (Symfony, Laravel, Codeigniter) which help you keep your code organised

Sign up to request clarification or add additional context in comments.

Comments

1

I've used PHP a lot in the past so I know what you mean. One thing that helps organize things more is following REST guidelines. So instead of api/post-actions and maybe api/get-actions you might have just api/actions. When you make a GET request, it returns actions. When POST and the body is one object, create one entity. If an array, create many. api/actions/:id would return one by id, PUT to the same would replace it, PATCH would update given fields, DELETE would delete, etc.

Another pattern I've used with large CakePHP MVC apps (often legacy non-SPA apps being converted to Angular SPA), that have perhaps a hundred or so models, is to create just one generic REST controller. Usually REST logic is so identical between different models, that the controller rarely even needs to be extended, but it easily can be, if eg. you need a custom method or custom logic in any REST action for one model, but not others.

Then add multiple routes pointing to the same controller, like say /actions/** and foobarmodel/**. Then just let the controller decide according to the URL, which model to call, and perhaps apply some conditional auth rules too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.