如何在PHP应用程序中使用Angular 2?

时间:2022-05-29 05:38:47

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?

我是一名PHP开发人员,我开始学习Angular2。但我不知道如何在PHP中使用它。是否有可能使用Angular作为前端?我该怎么办?如何在没有安装Node.js的共享主机上使用它?

4 个解决方案

#1


59  

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

我作为很长一段时间的PHP开发人员花了2美分,他一直在玩Angular2。

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.

作为PHP开发人员,您希望让PHP呈现HTML页面并发送给客户端。你不会用Angular2做到这一点。所有那些将在PHP中进行的处理,构建数据表,列表或其他任何现在都是Angular的工作。

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

你现在要做的唯一事情就是发送JSON响应。其他人已在上面说过这个。

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

我假设,因为你问这个你几乎没有使用Angular2的经验。所以这是交易:

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.

了解如何在本地计算机上使用Node和NPM。了解如何使用NPM设置空的Angular2项目。在本地计算机上播放和开发。

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

当您准备加载数据时,PHP可以通过将JSON数据发送到前端以供Angular使用来参与其中。

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.

当您准备好将Angular2应用程序放到世界各地时,您有许多构建选项。您需要将代码从Typescript编译为Javascript。

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

我一直在使用Angular CLI工具。这让我只需运行“ng build”,即可编译应用程序。

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.

然后我可以将它生成的文件夹上传到我的apache服务器并且它可以工作。 Angular CLI创建一个名为“dist”的文件夹,其中包含您的前端需要的所有内容。

Piece of cake.

小菜一碟。

#2


7  

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.

最好,您的网站只需下载一个简单的index.html和一个名为app.js的文件,其中包含您的所有JS,因此也包含您的Angular应用程序。

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.

PHP将坐在服务器上完成API的工作,它正在回答JSON / XML请求,然后您的角度应用程序将使用JSON构建Web界面。

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

您可以在任何地方托管PHP,并从其他地方为您的角度应用程序提供服务,甚至认为由于延迟而不建议使用它

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

#3


4  

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.

据我所知,AngularJS是客户端Javascript框架。所以一般来说不需要'nodejs'。您只需要HTML中包含的AngularJS库文件,这些文件将由您的PHP代码生成。

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

我想你最初应该仔细看看AngularJS。

#4


0  

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

以上[sitesbyjoe]最佳答案很棒。只是细节步骤,

open cmd

打开cmd

1) cd your-project folder

1)cd你的项目文件夹

2) ng build

2)ng build

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

3)将dist文件夹复制到apache / htdocs / [dist /或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

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

It works.

有用。

#1


59  

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

我作为很长一段时间的PHP开发人员花了2美分,他一直在玩Angular2。

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.

作为PHP开发人员,您希望让PHP呈现HTML页面并发送给客户端。你不会用Angular2做到这一点。所有那些将在PHP中进行的处理,构建数据表,列表或其他任何现在都是Angular的工作。

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

你现在要做的唯一事情就是发送JSON响应。其他人已在上面说过这个。

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

我假设,因为你问这个你几乎没有使用Angular2的经验。所以这是交易:

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.

了解如何在本地计算机上使用Node和NPM。了解如何使用NPM设置空的Angular2项目。在本地计算机上播放和开发。

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

当您准备加载数据时,PHP可以通过将JSON数据发送到前端以供Angular使用来参与其中。

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.

当您准备好将Angular2应用程序放到世界各地时,您有许多构建选项。您需要将代码从Typescript编译为Javascript。

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

我一直在使用Angular CLI工具。这让我只需运行“ng build”,即可编译应用程序。

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.

然后我可以将它生成的文件夹上传到我的apache服务器并且它可以工作。 Angular CLI创建一个名为“dist”的文件夹,其中包含您的前端需要的所有内容。

Piece of cake.

小菜一碟。

#2


7  

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.

最好,您的网站只需下载一个简单的index.html和一个名为app.js的文件,其中包含您的所有JS,因此也包含您的Angular应用程序。

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.

PHP将坐在服务器上完成API的工作,它正在回答JSON / XML请求,然后您的角度应用程序将使用JSON构建Web界面。

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

您可以在任何地方托管PHP,并从其他地方为您的角度应用程序提供服务,甚至认为由于延迟而不建议使用它

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

#3


4  

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.

据我所知,AngularJS是客户端Javascript框架。所以一般来说不需要'nodejs'。您只需要HTML中包含的AngularJS库文件,这些文件将由您的PHP代码生成。

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

我想你最初应该仔细看看AngularJS。

#4


0  

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

以上[sitesbyjoe]最佳答案很棒。只是细节步骤,

open cmd

打开cmd

1) cd your-project folder

1)cd你的项目文件夹

2) ng build

2)ng build

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

3)将dist文件夹复制到apache / htdocs / [dist /或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

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

It works.

有用。