I am developing an AngularJS 1.4 Application within a Symfony2 Bundle. Symfony provides the "backend" (API) and Angular the frontend (of course).
I am using the new router and stick to the components driven folder approach suggested by several guides and best practice examples. But since I build my JavaScript with gulp and only include the complete Build-Files there are issues with Angular controllers not finding their templates.
I show you my solution, which I don't like:
(function () {
'use strict';
angular
.module('myModule', ['ngNewRouter', 'myModule.dashboard'])
.config(TemplateMapping)
.controller('AppController', AppController);
/* @ngInject */
function AppController ($router) {
$router.config([
{ path: '/', redirectTo: '/dashboard' },
{ path: '/dashboard', component: 'dashboard' }
]);
}
/* @ngInject */
function TemplateMapping($componentLoaderProvider) {
$componentLoaderProvider.setTemplateMapping(function (name) {
return {
'dashboard': '/bundles/mybundle/templates/dashboard/dashboard.html'
}[name];
});
}
}());
I write my Angular Code in src/myBundle/Resources/js/
and gulp puts the final Build to src/myBundle/Resources/public/
which is then available in the Twig Template that holds the Angular app.
What I am doing right now, is basically putting the templates of my components not where they belong (that would be src/myBundle/Resources/js/components/dashboard/
for the dashboard example) but into src/myBundle/Resources/public/templates
.
I have to tell the router that the template is located elsewhere through $componentLoaderProvider.setTemplateMapping()
.
I have two Questions:
AppController
) where the template is?The cool thing with API based architectures is that you can uncouple the backend app (the API) from the frontend (the AngularJS app).
I recommend to create two separate applications for frontend and backend. Each application should be in its own Git repository and can eventually be hosted on its own server:
index.html
file, JavaScript files, CSS stylesheets, images...).The API is the contract between the two side and they can evolve separately. Maintenance is eased and the coupling is tight. The frontend app can even be hosted directly on a CDN such as Amazon CloudFront or GitHub pages for maximum performance.
I've written an article detailing this approach and presenting some tools I've built. It uses Symfony and AngularJS.
If you still want to have a single application:
app/frontend
web/
The best approach as Kévin mentioned is to separate your front-end and back-end in a way that even if you change the back-end to another technology you don't need to change your front-end.
There are also 2 git projects that can help you to have a nicer app.
1) FOSJsRoutingBundle
https://github.com/FriendsOfSymfony/FOSJsRoutingBundle Which allow you to use your Symfony routing in your JavaScript code
2) angular-schema-form
https://github.com/json-schema-form/angular-schema-form Which is a dynamic form generator. It creates and validates a form from a JSON schema.
So what I did was to create a Angular Schema Form Serializer in Symfony to convert a Symfony form to a Jason schema which can be used by angular-schema-form to generate my forms dynamically. In this case I don't need to implement form in html. Even if I change the API I just need to provide the same Jason schema for my forms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With