1

I come from a PHP background and I have used traditional JS frameworks like Jquery and Angular 1 to some extent. I just started learning Angular 2. I have gone through multiple sites and demos, code generators like angular-cli, vulgar etc. and all of them work as expected. No problems till now.

I work on windows. So whenever I need to run any angular 2 demo application, I need to run at least 2 or 3 commands on different command prompts and they all need to be continuously running like ng serve, npm start, gulp etc..

Suppose I want to create a simple php application with 3 php files. Page 1 is where the angular2 app should be running. Within Page 1, there will be links to page 2 and page 3 php files along with additional routes that with be shown using angular2 routes.

Now since all demos I have seen use typescript, how should I do it in php?

I have used angular1 with PHP and it was as simple as importing a script file.

Can I just import some script files and have the angular2 app running within a php page? Do I have to run all those(npm,ng,gulp) commands to have the angular2 app running on a php page?

Jk Jensen
  • 339
  • 2
  • 4
  • 16
Santhosh Shet
  • 97
  • 1
  • 3
  • 11
  • yes you do, when you use npm package manager it keeps track of all the files and libraries you fetch; in-case you decide to take it down, it'll remove the package without impacting the application. it's a good practice to have them installed to keep good formation. – unixmiah Jul 08 '16 at 18:43
  • Running those commands is not a problem. But to use angular2 do we need to have node js running?. If this application was hosted on a lamp server, the php file would be processed by Apache. My assumption is angular2 would not need any additional process on the server to run – Santhosh Shet Jul 08 '16 at 19:21
  • No you don't. You can do the following on apache http://www.thedevline.com/2014/04/how-to-run-angularjs-project.html. You need some type of web server to serve the files in an orderly fashion. – unixmiah Jul 08 '16 at 19:56

1 Answers1

0

Concerning the TypeScript part:

Now since all demos I have seen use typescript, how should I use it in php?

...remember that TypeScript is a superset of JavaScript, and transpiles to JavaScript. So you might have a process that transpiles your TypeScript app code to JavaScript before you load your page, and runs it in the browser as JavaScript. Or, you can also run TypeScript in the browser (see this Stack Answer for more info).

When you run through the Angular 2 tutorials, you will see that as you are writing TypeScript, it creates JavaScript files for you. The app that runs in the Tutorial (using Node.js) is serving JavaScript, not TypeScript. So, as you mentioned:

I have used angular1 with PHP and it was as simple as importing a script file.

Nothing will change there. The only additional step is getting the TypeScript transpiled to JavaScript. For that you can checkout SystemJS (that's what the Angular 2 turotials use) or WebPack (which the Angular 2 also has docs on here, which both have plugins/bundlers for doing that for you.

Community
  • 1
  • 1
User 1058612
  • 3,739
  • 1
  • 27
  • 33
  • OK. What I understand is using web pack or other tools we need to capture the generated js files and use them. This way I can avoid running those commands constantly. Is this correct? – Santhosh Shet Jul 08 '16 at 19:14