A
A
Alexander Evgenievich2015-05-29 13:16:25
PHP
Alexander Evgenievich, 2015-05-29 13:16:25

Is it possible and necessary to pass a JSON object from PHP to ANGULAR in a custom HTTP header?

Hello.
I apologize for the possibly stupid question, but still. Passing data from PHP to angular is a pain in the ass unless you're working with SPA . I have twig templates. At the moment I use my directive to transfer data from PHP to scope (ng-init also considered). There is no desire to make many requests to obtain data. So I thought that if you pass a JSON object with data for a specific page in your X-Custom-Header header, and read them and transfer them to scope. I would like to hear your opinions, maybe you know other ways to pass data from a PHP template to angular.
----
Guys, let's go! I didn't know that JS can't get the titles of the initial page.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Shikanov, 2015-05-29
@banderos120

This is definitely not according to REST and, accordingly, there will be problems with caching, besides, it is not known how proxy servers will behave with such headers, you can hit the limit and the proxy will simply cut them.
All in all, it's a bad idea in my opinion.

M
Mikhail Osher, 2015-05-29
@miraage

Easy.

<!-- тег должен лежать до загрузки скриптов приложения -->
<script>
window.userInfo = <?= json_encode(Session::get('user')); ?>;
</script>

<!-- внутри приложения -->
<script>
app.config(function($provide) {
  $provide.constant('userInfo', angular.copy(window.userInfo));
});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question