How to integrate PHP with yeoman angular project

风流意气都作罢 提交于 2019-12-07 10:13:54

问题


I'm using yeoman project using angularjs normally I know how to use angularjs with PHP on normal projects.

But I'm confused to use php with yeoman.

Where should I create .php file and how should I call $http scope in main.js controller?


回答1:


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.




回答2:


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/



来源:https://stackoverflow.com/questions/19633003/how-to-integrate-php-with-yeoman-angular-project

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