Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Angular 2 in PHP application?

Tags:

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?

like image 902
M Gholami Avatar asked Aug 02 '16 10:08

M Gholami


People also ask

Can Angular be used with PHP?

Using PHP With Angular If you're wondering whether you can use PHP alongside Angular, the answer is yes. But Angular will still need a separate client-server architecture. In general, PHP runs on the server-side while Angular runs on the client-side.

Which language is used in Angular 2?

Angular 2 itself is built using TypeScript.

Is Angular 2 better than AngularJS?

Angular 2 is a newer version of AngularJS, released in 2016. This version of the framework using TypeScript, which is an open-sourced programming language maintained by Microsoft. Angular 2 is more useful for developing mobile applications and includes higher performance speeds than AngularJS.


2 Answers

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.

like image 74
sitesbyjoe Avatar answered Oct 03 '22 17:10

sitesbyjoe


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>
like image 28
Borjante Avatar answered Oct 03 '22 19:10

Borjante