Q
Q
Quber2014-10-10 06:28:43
JavaScript
Quber, 2014-10-10 06:28:43

How to connect AnularJs Route and Symfony Routing?

There is a page address: test.ru/post/153245
Loading a page using the angular route

$routeProvider.when("/post/153245", {
            templateUrl: "/post/153245",
            controller: 'MainController',
            controllerAs: 'main'
        });

Everything is working. But what if the user gets to the site directly at this address? The template will load and then angular will load it a second time. How to avoid it?
Symphony route:
post:
    path: /post/{id}
    defaults: { _controller: AcmeCoreBundle:Post:index }

twig template
{% extends '::layoute.html.twig' %}

{% block content %}
    MAIN
{% endblock %}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
agladkov, 2014-10-21
@Quber

Despite the fact that everything was decided in the first question, I will leave my solution to the original question.
First, we ask Angular to send the 'X-Requested-With' header when making requests to the server:
Accordingly, now on the server side we always know whether this is the initial loading of the template, or a request from Angular:
If it is the initial loading, then we give the base template, for example '::layoute.html.twig', in which the area for the template of a particular page is wrapped in ngView.
If the request is from angular, then we process the request and return the prepared template for insertion into ngView.
We do this because we are generating a template based on the user's permissions.
We give data for the template through ngInit.
Such a scheme also makes it possible to work with forms quite simply: generate with all the necessary attributes, send the symphony in the correct form with angular and, accordingly, display the processing results.

A
apasen, 2014-10-10
@apasen

Once I already had such an idea, but after much thought on this topic, I realized that this should not be done at all. And it is better to separate the backend from the frontend. Create a REST application. Those. the frontend is a separate application in angular with its own routing, and the backend, in this case written in symfony, is an implementation of the API. But if there is an urgent need to use the symfony route in angualr, there is a FOSjsRoutingBundle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question