Search code examples
angularjsyeomanyeoman-generator

How to integrate PHP with yeoman angular project


I'm using project using 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?


Solution

  • 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.