M Gholami
M Gholami

Reputation: 971

How can I use Angular 2 in PHP application?

I am a PHP developer and I started learning Angular2. But I don't know how to use it with PHP. Is it possible using Angular just as frontend? What I must to do? How to use it on shared hosting without Node.js installed?

Upvotes: 22

Views: 35081

Answers (4)

hoogw
hoogw

Reputation: 5525

above [sitesbyjoe] best answer is awesome. Just details steps,

open cmd

1) cd your-project folder

2) ng build

3) copy dist folder to apache/htdocs/[dist/or your-project-name]

4) Important:  open the index.html file, find <base href="/">
change it to <base href="/your-project-name/">
Without doing this, js file will not be load correctly.

5) http://localhost:80/your-project-name/index.html

It works.

Upvotes: 1

sitesbyjoe
sitesbyjoe

Reputation: 1861

My 2 cents as a long time PHP developer who has been playing with Angular2 quite a bit.

As a PHP developer, you're expecting to have your PHP render an HTML page and send to the client. You won't be doing that with Angular2. All that processing that would take place in PHP, building data tables, lists, or whatever is now Angular's job.

The only thing you're gonna do with PHP now is simply send JSON responses. Others have said this already above.

I'm assuming that since you're asking this you have little to no experience working with Angular2. So here's the deal:

Learn how to use Node and NPM on your local machine. Learn how to use NPM to set up your empty Angular2 project. Play and develop on your local machine.

When your ready to load in data, PHP can get involved by sending JSON data down to the front end for Angular to use.

When you're ready to put your Angular2 app online for the world you have a number of build options. You need to compile your code from Typescript to Javascript.

I've been using the Angular CLI tool. That lets me just run "ng build" and the app gets compiled.

Then I can upload the folder it generates up to my apache server and it works. The Angular CLI makes a folder called "dist" that contains all the stuff your front end will need.

Piece of cake.

Upvotes: 66

Borjante
Borjante

Reputation: 10517

Preferably, your site will just download a simple index.html and a file called app.js which contains all of your JS and therefore your Angular app.

PHP will be sitting on a server doing the job of an API, which is answering with JSON/XML to request, you angular app will then use the JSON to build the web interface.

You can have PHP hosted anywhere, and serve your angular app from another place, even thought it's not recommended because of latency

<html>
  <script src="app.js">  
</html>

Upvotes: 7

inzanez
inzanez

Reputation: 366

as far as I know AngularJS is client side Javascript framework. So in general there's no need for 'nodejs'. You just need the AngularJS library files included in your HTML that's going to be produced by your PHP code.

I guess you should have a closer look at AngularJS at first.

Upvotes: 4

Related Questions