How to integrate PHP with yeoman angular project

狂风中的少年 提交于 2019-12-05 17:54:32

If you're using generator-angular, you need two elements:

1/ Have your PHP running as you would normally in a subdirectory like public so public/index.php is loaded when you go to http://localhost/

2/ Use a Grunt task to compile the individual AngularJS source files in src into a single file somewhere like public/js/myapp.js and in public/index.php add something like <script src="/js/myapp.js></script>

3/ If you're wanting to send JSON to your angular app, use json_encode such as:

<?php
header('Content-type: application/json');
echo json_encode($someArray); ?>
?>

You could also have a look at grunt-php for serving up you PHP files on a dev box.

Shameless self plug - I've also written a generator that creates a simple AngularJS app with a FlightPHP backend: https://npmjs.org/package/generator-flightangular - Which might give you an idea of how you can split the JavaScript elements from the PHP backend.

You could use web service API calls to your PHP project. Usually PHP and AngularJS are two different scopes. We can establish communication between these two using API's. Normally it is better to use some REST API frameworks like Slim.

http://www.slimframework.com/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!