How can I use Angular 2 in PHP application? - php

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?

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.

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>

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.

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.

Related

How can I connect my React app to a PHP file?

I'm trying to connect a simple contact form to send emails using React and PHP.
The problem I have is that every tutorial or article I've found either leaves out key parts that probably seem simple yet to a complete PHP beginner are all the difference between a working app or not. I have basically no experience with PHP and connecting a PHP file to a React app, I've downloaded Docker and Apache (yet have no idea how they fit into the picture), where to place the PHP file (if it even matters), how to host the PHP development server, etc...
I'm aware I should ideally spend some time reading through and actually learning PHP, it's something I plan to do, yet I'm pressed for time so that isn't currently an option.
The code I'm using is from this article https://blog.bitsrc.io/how-to-build-a-contact-form-with-react-js-and-php-d5977c17fec0
I currently have the PHP file placed like so: projectfolder -> php-folder -> index.php
and the React like so: projectfolder -> react-folder -> src --> (all the react files)
again, all the code is from the tutorial I linked and all I did was place the PHP from the article into the index.php file like above, and the same thing with the React code from the article (with the path names corrected) so I'm sure it's something I'm missing with the backend like setting up Docker or Apache, or possibly the file placement.
Sorry if this seems like a really broad question, essentially I just need to know the steps for correctly connecting a PHP file with a React app to send emails from a contact form.
Thanks

Aurelia + Php Possible/Recommended?

Before this question gets closed, I know the setup above is possible. I just want clarification on some things.
I just started learning Aurelia because I want to convert one of my projects into a web app. My project is built with html+css+JavaScript(jQuery)+ PHP(MySql).
I havent used any sort of framework before.
In the guide, they mention a few ways to setup a web server. I used the http server with node. Now this is where I need some help understanding a few things.
I dont want to use node.js. I want to use PHP on the server. Will that work and how?
When using Apache server, I know any PHP page is sent to the interpreter that renders the final html. I use XAMPP and its apache comes bundled with PHP. Does the http server used by node come with PHP? Is this even a sensible question?
Now I know Aurelia is purely front end. If it used to make single page applications, it uses Ajax. So now I made the following assumption:
Using Aurelia, the user accesses the root page of the app that the web server sends. After that, Aurelia makes various Ajax requests to the server which will use my PHP files to do database query stuff.
Is that right or am I missing something. And can I just use xampp(apache) to host my app instead of server from node?
Aurelia is a framework that, after you export it to any server, does not rely on any back-end software at all. This means that with the help of the http- / fetch-client API, you can just call out to your php script.
I have an example in my github:
https://github.com/rjpvroegop/randyvroegop.nl-made-with-aurelia
Here I use the http-client to post data to my php script wich has a very simple email functionality.
You can see the action inside my view-model in src/pages/contact/index.js.
You can see the PHP script in src/assets/components/contactengine.php.
These work the way they should. Note: you have to change your gulp build if you want your PHP served the way I serve mine, from the dist folder after gulp-watch or gulp-export.
Next to that you can use any back-end functionality you would like, as long as it returns the proper data. This PHP script does that. If you would download my distribution to test this you can simply do the following:
gulp export from your terminal in the root folder
copy everything from the export folder to your PHP webserver.

Connecting to SimPro API via Web

This is a very similar to the question posed at Use php and simPro API to list customers.
The response to that question suggests downloading examples from GitHub. I have downloaded the SimPro examples at GitHub and have them functioning on the commandline.
I want to be able to use a web page as an intermediary between my FileMaker Pro database and the SimPro API. I can pass data to a web page written in PHP. It can convert the data to JSON and form a call to SimPro receive a response and display the success or failure as a web page.
Presently I have my JSON data hardcoded so that I can test the process. When run from a web browser I don't get a result, though the code works perfectly well running from the command line.
I'm not sure what I need to do make the examples web compatible. Can someone push me in the right direction?
Would be more than happy to help - could you post a snippet of your code - minus the auth info of course - so that I can try to assist?
Do you have your code printing anything else to the browser first - so that you make sure the script is actually executing as it should from the web server side - and have you checked your web server logs for any errors?
In some cases I've seen this is usually due to PHP config (there are separate configs for CLI and CGI in most setups) and can mean whilst libraries are loaded in one environment they may not be available to the other. The web server also usually requires a reload if you have just loaded libraries for use within your script.
Hope that helps.

AppMobi / Phonegap Alternative with PHP

Not really a coding question exactly, and not sure which stack site to throw this on, so here goes.
Im wondering if there is any framework similar to appmobi/phonegap in the sense everything is sandboxed and compiled into an app format for both iphone and droids. Where the sandboxed server comes with the ability to run PHP on it, I've been tryin to search all day and I've come up with nothing as of yet other then heaps of articles on appmobi/phonegap. This could be a free or paid for framework (preferably free/open source).
If theres no frameworks like that then is there a means of taking something like apache itself that acts and runs like a webserver where I can load php into it, and at the end of my project compile it into one final package for use in the mobile markets?
The problem is that iOS generally uses Objective-C and the Android uses Java, but both can use javascript, which is why phonegap works.
So, unless you can write two webservers, one for iOS and one for Android, and port PHP to run under your webserver, or, more likely, write an interpreter that will take PHP and transform it to run on your webserver, then it would work.
But, if you write a mobile web application and use PHP as the code on the server, then you can send javascript and have that run in a browser on both devices.
The best option would be to have most of your business logic be in REST services that are written in PHP. You can do this in PhoneGap by following this blog:
http://share.ez.no/blogs/thiago-campos-viana/rest-api-basic-http-auth-and-phonegap-using-jquery
Here is the main part of the article though, so you can see how easy it can be.
//10.0.2.2:80 is the localhost in android emulator, app needs internet access
$.get("http://10.0.2.2:80/ez_site/api/ezp/content/node/2",
function(data) {
//code goes here
});
By doing this then you can have most of your business logic in PHP, and have just the UI be in javascript, able to take advantage of the hardware on the phones.
No. Attempting to include a web server and PHP runtime inside of a mobile application would have serious performance implications.
The reason PhoneGap works is because Javascript can execute natively in mobile applications by using the built-in web browser's rendering engine.

Call PHP code from Python

I'm trying to integrate an old PHP ad management system into a (Django) Python-based web application. The PHP and the Python code are both installed on the same hosts, PHP is executed by mod_php5 and Python through mod_wsgi, usually.
Now I wonder what's the best way to call this PHP ad management code from within my Python code in a most efficient manner (the ad management code has to be called multiple times for each page)?
The solutions I came up with so far, are the following:
Write SOAP interface in PHP for the ad management code and write a SOAP client in Python which then calls the appropriate functions.
The problem I see is, that will slow down the execution of the Python code considerably, since for each page served, multiple SOAP client requests are necessary in the background.
Call the PHP code through os.execvp() or subprocess.Popen() using PHP command line interface.
The problem here is that the PHP code makes use of the Apache environment ($_SERVER vars and other superglobals). I'm not sure if this can be simulated correctly.
Rewrite the ad management code in Python.
This will probably be the last resort. This ad management code just runs and runs, and there is no one remaining who wrote a piece of code for this :) I'd be quite afraid to do this ;)
Any other ideas or hints how this can be done?
Thanks.
How about using AJAX from the browser to load the ads?
For instance (using JQuery):
$(document).ready(function() { $("#apageelement").load("/phpapp/getads.php"); })
This allows you to keep you app almost completely separate from the PHP app.
Best solution is to use server side includes. Most webservers support this.
For example this is how it would be done in nginx:
<!--# include virtual="http://localhost:8080/phpapp/getads.php" -->
Your webserver would then dynamically request from your php backend, and insert it into the response that goes to the client. No javascript necessary, and entirely transparent.
You could also use a borderless <iframe>
I've done this in the past by serving the PHP portions directly via Apache. You could either put them in with your media files, (/site_media/php/) or if you prefer to use something more lightweight for your media server (like lighttpd), you can set up another portion of the site that goes through apache with PHP enabled.
From there, you can either take the ajax route in your templates, or you can load the PHP from your views using urllib(2) or httplib(2). Better yet, wrap the urllib2 call in a templatetag, and call that in your templates.

Categories