I recently started learning PHP and Symfony 4 and I developed an app. Now my next task is to change the architecture of the project, from MVC to client-server. More exactly, I need to have my templates and controllers in an application and the business logic in another application. Any ideas how I can do this? I can't even figure out how to run 2 different symfony projects simultaneously.
A client-server application is an application base on a communication model call client-server: wiki
MVC is a way to develop and structure application: wiki
But i understand your needs. You want to keep a symfony app but you also want to put all the logic view and front end features into a separate app (like React or Vue)
Your Symfony App will still use MVC pattern, just you'll replace what the controller give to the view.
The best way is to turn the Symfony App into API with normalize view data like JSON or XML. Those data will feed your front app.
To achieve this you can use this bundle, which turn your model layers into API: API Platform
Related
I am working on a ERP system that is 6 years old and it is developed in raw PHP maintaining procedural coding structure without OOP or any Framework.
Finally our management decided to re-write the application with upgraded technologies. We will write the application in Laravel Framework. Database will be Oracle and Mysql both. In the front end we will use Angular 4.
The confusion is should i use Angular 4 for front end request handling and data binding? Or i should go with Javascript/Jquery. Last few days i am seeking some suggestions on the net. I found some also. My question is, how should i integrate Laravel and angular? should i go for API based approach maintaining two different project for Laravel and Angular or angular inside Laravel in a folder? If i go for API based approach, will my ajax request increased? The database of the application will be too large.
will API based approach make data loading slower?
All the request in the application will be ajax based and there will be no page refresh approach, but url will change on request.
Please suggest me the possible right solution to help me taking the right decision.
Angular 2/4+ has been designed to build SPA (Single Page Application). If you deep dive into angular then it will be clear to you that all the structures and conventions followed in the angular 2+ architecture are optimized for SPA.
So, if you want to use Angular with Laravel 5+ then I think the best way would be to use angular to build a standalone frontend application and use Laravel 5+ as a backend. That means laravel will act as an API server and a backend of the application.
Using laravel as an API server doesn't necessarily mean your database will be bigger. It is actually upto you how you design your application.
But, there is an another good solution.
You can use VueJs. There is a build in support for VueJs within Laravel 5+.
The benefit of using VueJs is that you can use it just like Jquery. That means you don't have to create separate standalone application for VueJs like Angular.
But, you can use it, page by page basis. It is loosely coupled.
You will get most of the benefit of Angular from it.
You can also use React.
You can use Angular 4 to rewrite your application. Fontend as Single Page Application and Backend exposing REST API will help you to build a good performance application.
I prefer to keep Angular and REST API separate as they should be independent to each other. REST APIs should always be generic so that they can be consumed by other applications too.
Yes, there will be lot of API calls to server. You can scale your REST APIs according to your number of requests. It is better building micro-services to ensure most of the services will not be impacted in case of lot of heavy load (at least part of application will be running). It will help you in future maintenance also.
I'm currently designing a new web application based on Symfony Framework that will serve both web and mobile accesses. For illustration purposes I will use the default AcmeDemoBundle from Symfony`s framework to elaborate my question.
So, DemoBundle gives me the following route:
/hello/{name}
So far so good. Now, I want to implement an API in this project that will serve mobile apps, so the route for the same controller as the route above whould be, for instance:
/api/v1/hello/{name}
My doubt is: what's the best way of doing that without replicating code? I intend to use the FOSRestBundle for the API and I know that he handles rendering HTML views to, but I'd like to separate the API routes from the web as I put before. Maybe should I create a new bundle for the API?
Create a bundle for the API is not a good practice for the simple reason that you can't easily re-use the bundle in an other project.
For example, you create PostBundle and his API bundle, APIPostBundle for your A project.
If you want to use that bundle for a B project, you need PostBundle and APIPostBundle. You created dependency between two bundles and it's a bad practice (Component-Based Development).
So, you have to generate an independency bundle with a strength architecture who can allow you less modification if you have to.
Use a service for the logical instructions. Then, the controller/APIController can manage the route and will call the service methods. By that way, you can just modify the service without editing the controllers.
So, your architecture looks like that:
PostBundle
Controller
- PostController
- APIPostController
Service
- PostService
- PostServiceInterface
So the past month ive been working alot with CakePHP getting to know the conventions also getting better at the MVC structure and how it works.
Now in cakephp you basicly bake Model views and controllers where Models are assoiciated to Database tables and controllers handle the request back and forward from the Model and the view.
Now Joomla is build in a different way. As far as ive understood Joomla is build up of modules and components but these components and modules follow the MVC structure.
Now to my Question:
How close is cakephp programming to Joomla programming like how does it compare? is creating components and modules the exact same thing as creating Models and Controllers in cake (execpt from the Api calls not being the same)?
The Joomla Framework has its own MVC just like all frameworks. If you develop applications on that it is somewhat different than what you would do building an extension inside the CMS.
Building an extension on the Joomla CMS is different from building an application on the framework and I really would separate that out and figure out what you want. IF you want to build on the CMS because htat give you user management, authentication, cache etc and then you build on top of that, it is a different than than building a stand along application.
So you really need to figure out which it is you want to do.
I have a web application which has been developed with symfony 1.4. I have a pretty large code base (and growing). Circa 80,000 lines of code (actions, forms, models, templates etc.)
I'm using the default doctrine version which ships with symfony 1.4.
I've just started developing a mobile version using Sencha touch. I don't wish to use symfony for the REST web services because:
REST services in Symfony 1.4 is not great. For example, If i want a PUT request I have to pass a 'sf_method' parameter specifiying that the request method is PUT. This isn't true REST and it's not ideal for Sencha touch.
I don't need all of the unnecessary symfony functionality(for example the plugins that are autoloaded in the ProjectConfiguration file, the form framework etc.) that you'd use to develop a standard web app. All I need is to define my REST routes and return the specified JSON (as everything that needs to be returned for Sencha touch will be JSON)
I want to keep my mobile app as bloated-free, efficient and quick as possible. And unfortunately for this task, Symfony 1.4 would not be the best choice for using as the backend architecture for my mobile app. If I had chosen symfony2 (it was in it's beta phase, alas) it would be a different story as symfony2 supports true REST functionality. What I do need, however, is the ability to use my current doctrine models (I have circa 90 models) in a chosen REST framework.
Basically, in a nutshell what I need is as simple as this:
Call a rest route->Query my doctrine models->return the JSON without using symfony.
So my question, what would be your advice? I don't want this to be a question of which is the best PHP rest framework, however, what I would like to know is what would be a good REST framework which i can develop efficiently and quickly REST service, make use of my doctrine models and is easily extendable.
Here at my employer, I've created a rather big application with a ExtJS frontend, and Symfony 1.4 backend. And two be honest, I don't feel limited by Symfony 1.4 in any way?
First of: I created my own base controller class (which extends sfActions). This controller can handle (render) different types of data. It has generic handling for Doctrine_Query, Doctrine_Collection, Doctrine_Model and array types.
Also the plugins make me help organize the code, and in some cases plugins are shared between differend projects, so that's also a big plus.
And the extra functionality like forms: it's only prepared for you in the autoloader, you don't have to use it. And I don't think it causes any real performance issues (at least not for me). But I like to use the extra sfValidator framework, to make sure data are correct.
The only real "problem" is indeed the HTTP REST-ful commands, especially PUT and DELETE. I just worked around this problem by generating a controller for each 'manageable' model, and implement specific get, list, create, update and delete actions. So when I would like to manage an Object, I call the objects controller, which has executeCreate, executeUpdate and executeDelete actions.
The reason I read, was that Symfony didn't and couldn't implement this feature because PHP has really bad support this. I don't know if this is true, but if this is your only 'real' issue, you could try to fix this in the Symfony core.
So my advice:
If the raw performance is your problem: try profiling your code, install a opcode (APC) cache, and profile your code (yes, that's double).
If the HTTP PUT command is your problem: I would either work around this (that's the way I solved it), or try to fix it in the core.
I am embarking on creating a Yii project that basically has a shared data model (let's say CerealStuff) with a front-end web site (call it site), admin site admin, and web service ws. I am new to the whole MVC thing and I wanted to know how I should design this project to stay consistent with Yii/MVC best practices. So far I have identified two basic options:
Create models CerealStuff at root, create three modules site, admin, and ws each with their own controllers;
Create models CerealStuff at root, create two modules admin and public with public containing controllers that handle Site and have #soap declarations to handle ws stuff.
I know that option 2 reduces the total amount of reproduced coding but it does not feel as clean quite honestly. Also I feel like maybe a modern web app should be such that even the "site" (view) uses the web service to access the database.
Tell me what to think!
I'm exposing a simple web service in my app, and I went with a separate (soap) controller.
If you follow the fat model / skinny controller paradigm, it's pretty simple to tack on a web service front end to your models.
Edit: better example of fat models: http://www.therailsway.com/2007/6/1/railsconf-recap-skinny-controllers
Read this article to see an example of MVC structured Yii project with two entrance points for front-end and back-end. Once learned this you can add another entrance point easily for ws.
As you can see all models are shared between modules. Controllers, config files and views are separate. Views can be configured as shared as well. I used this type of structure in many projects and never had any issues with extending or scaling.