Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP app #1484

Closed
grafikkaos opened this issue Apr 24, 2015 · 50 comments
Closed

PHP app #1484

grafikkaos opened this issue Apr 24, 2015 · 50 comments

Comments

@grafikkaos
Copy link

Is there a way I can create a PHP app with this?

@supertanuki
Copy link

I don't think it is the purpose of Electron.
You can write your own backend or API in PHP that will exchange data with the desktop electron app.

@steelbrain
Copy link
Contributor

Well, I don't think a point in creating an Electron App in PHP. I mean, I create apps in Javascript for Electron and in PHP for Servers.
Not only it'll complicate stuff, but it doesn't make any sense, really. I mean, we'll have to embed a php FCGI server within electron, then use a node-js fcgi client and then execute the bundled files, even then our primary language would still be javascript.

@zcbenz
Copy link
Member

zcbenz commented Apr 25, 2015

There is no support of PHP in Electron.

@zcbenz zcbenz closed this as completed Apr 25, 2015
@andrewrobinson5
Copy link

It would be nice if php was a thing here, but it would be all the more difficult because of servers. There are many js things for it though. I will try to find a MySQL script.

@steelbrain
Copy link
Contributor

@Cheapskate01 PHP wasn't built for this purpose and it would be abuse of it to use in this way. We must try to learn new languages instead of abusing existing ones. Node can do everything php can and is more suitable for an electron like use-case where we need to do async stuff and we just can't afford to block the event loop.

If you are new to node, I would suggest you have a look at this cheatsheet and for MySQL You can use node-mariasql or for a nicer API mariasql-promise.

@andrewrobinson5
Copy link

Not yet, but I'm sure i can find a way if i try

On Mon, Oct 26, 2015 at 9:57 AM, gbaumgart notifications@github.com wrote:

@grafikkaos https://github.com/grafikkaos you found a way to embed a
PHP server?


Reply to this email directly or view it on GitHub
#1484 (comment).

@PrestaSafe
Copy link

Hi everybody, i've embed php in electron is someone is interesting
it's only in french but i can help
http://base2code.com/creer-une-application-native-avec-html-css-js-et-php/
Thanks

@andrewrobinson5
Copy link

Looks great!

On Wed, Feb 17, 2016 at 7:05 AM, guillaume23 notifications@github.com
wrote:

Hi everybody, i've embed php in electron is someone is interesting
it's only in french but i can help
http://base2code.com/creer-une-application-native-avec-html-css-js-et-php/
Thanks


Reply to this email directly or view it on GitHub
#1484 (comment).

@baconbrad
Copy link
Contributor

I have embedded it in an NW.js example as posted on https://github.com/baconface/php-webkit and I recently embedded it into Electron when I converted my editor at https://github.com/baconface/tweaked so I know it is possible. Give me a few days and I convert my NW.js example to Electron when I get a chance.

@2braincells2go
Copy link

@baconface You ever get any where with this? Got a classroom project that really like to try out.

@andrewrobinson5
Copy link

I ended up using an iFrame to a remote server for stuff like login and register forms that require sql

@2braincells2go
Copy link

2braincells2go commented May 12, 2016

@Cheapskate01 yes that is great solution, wish could use but classroom project cannot access the internet. no need for sql on this one...

@andrewrobinson5
Copy link

Ah. Well, the other (less secure) solution I found is to make everything in
a localhost server and have electron load that. Because you don't need to
handle an sql password, you can use this pretty easily.

On Thu, May 12, 2016 at 9:14 AM, Woody notifications@github.com wrote:

@Cheapskate01 https://github.com/Cheapskate01 yes that is great
solution, which I could use but classroom project cannot access the
internet. no need for sql on this one...


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1484 (comment)

@florianb
Copy link

It seems like the usual (and secure) way to solve your problem is to provide an API on your server (maybe written in PHP) and use your Electron-app to display a dedicated interface to your server side services.

@andrewrobinson5
Copy link

I can honestly say I have no clue how to do that. Though it is a good idea.

On Thu, May 12, 2016 at 9:29 AM, Florian Breisch notifications@github.com
wrote:

It seems like the usual (and secure) way to solve your problem is to
provide an API on your server (maybe written in PHP) and use your
Electron-app to display a dedicated interface to your server side services.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1484 (comment)

@2braincells2go
Copy link

Thanks all of input. Since this is for use in classroom environment where we are "testing & learning", not worried about security at this point. Just something to "try & learn" from mistakes...

@andrewrobinson5
Copy link

Yeah, well for what you need it for, A localhost server is a good idea.
It'll give your students a chance to use and learn basic PHP.

On Thu, May 12, 2016 at 9:57 AM, Woody notifications@github.com wrote:

Thanks all of input. Since this is for use in classroom environment where
we are "testing & learning", not worried about security at this point. Just
something to "try & learn" from mistakes...


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1484 (comment)

@2braincells2go
Copy link

2braincells2go commented May 12, 2016

@Cheapskate01 already have a local server, what computer lab would not have one? The point in asking about PHP and Electron was to "hack around" and allow everyone to learn Electron in other aspects. Student brought the subject of PHP and Electron, so my job to explore:) Thanks for your input.

@andrewrobinson5
Copy link

Alright, no problemo

On Thu, May 12, 2016 at 11:23 AM, Woody notifications@github.com wrote:

@Cheapskate01 https://github.com/Cheapskate01 already have a local
server, what computer lab would not have one? The point in asking about PHP
and Electron was to "hack around" and allow everyone to learn Electron in
other aspects. Thanks for your input.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1484 (comment)

@andrejuseu
Copy link

@Cheapskate01 and @2braincells2go like other people said, Electron is only javascript friendly, why would you try enabling PHP server to work in simple app, that makes no sense.

Read about API's creation in PHP, and then connect electron to your API. Simply output given API data in APP and that's it.

@PrestaSafe
Copy link

@andrejuseu i understand your point of view. For me it could be great for proposing an app really cross plateform. Desktop, and web without rewriting the server side and db transaction

@steelbrain
Copy link
Contributor

@guillaume23 Can you provide a real world use case where you would want to bundle the back-end with the frontend? IMHO it wouldn't be a good idea to bundle the back-end with the app. And even if you still want to do that, then write the back-end in node.js and you'll be able to bundle it as-is in Electron.

If you still want to use PHP and bundle it in the app, which is a terrible terrible idea IMHO because rewriting the php parts in node.js is worth it for various reasons, then please use some alternative like Tide SDK.

IMO Bundling PHP support in Electron would be bloatware. Sure you can use a third-party node module to bridge between php and node but integrating it into Electron would mean that the 99.9999% people would get bloatware that they would never use.

A more sane alternative to this would be to have a PHP app that exposes an API which you can interact with from Node.js.

@baconbrad
Copy link
Contributor

I don't personally bundle PHP applications into desktop apps. But as the creator of the PHP-Webkit NW.js boiler plate I can tell you a lot of people do. That project alone has 61 stars on Github as of now. There are actually several PHP desktop app containers out their with pretty decent user bases.

If it comes to writing the desktop project from scratch I wouldn't use PHP since I know JavaScript and know Electron well. However a lot of users wanting this are wanting to interact with the machine using PHP for one of several reasons.

  1. The application is already written in PHP. And there is no concerns of exposing the server side to the client side.
  2. They are a strong PHP developer. And prefer to stick to PHP as their language. (Keep in mind it isn't insane to use a backend language like PHP. Node.js after all is considered a backend language too.
  3. Who knows. Who cares.

So I agree PHP support doesn't need to be added to Electron. But if someone wants to then all the power to them.

I am currently rewriting PHP-Webkit from a boilerplate to be an actual NPM module an Electron or NW.js user can simply require and include PHP projects a little bit easier. Unfortunately it is a time permitting kind of thing.

@samifouad
Copy link

samifouad commented May 29, 2016

I have to disagree with you @steelbrain.

PHP's entire purpose was to enhance static HTML pages. Electron lets you serve static HTML pages. There is a extra overhead to enabling PHP in Electron due to the JavaScript-oriented nature of the system, but it's not fundamentally wrong.

PHP isn't just for servers/backend stuff. It excels at that, but it can be primarily used for both client-focused stuff and backend-focused stuff. Just like JavaScript is now thanks to Node.

It makes sense why someone would want to enable processing of PHP files because a ton of websites, web apps and open source projects (Wordpress, MediaWiki, etc.) use PHP. If Electron is being billed as a way to run web apps on the desktop, enabling pre-processing of HTML makes sense even if there is a slight resource overhead for doing the pre-processing for every page load.

I think it's not the most efficient method to get the job done using Electron, PHP apps would probably be slightly out-performed by an identical app that's undergone a JS rewrite, but given the pros vs a slight performance hit, it makes sense for this to be a thing. Especially if it's a stepping stone for people to actually do the rewrites in JavaScript. Think of it kind of like training wheels for fully adopting this much more modern app distribution model.

@johnpwarren
Copy link

Can confirm, is bloatware. That said, I've bundled PHP in Electron with Docker containers. It works and gives an interesting mix of capabilities at the expense of performance. That said, I'm leaving PHP alone in the client front end and writing it in JS, just leaving the existing PHP rendered pages for admin/power user section of the app so I don't have to rewrite all that.

@theBliz
Copy link

theBliz commented Sep 3, 2016

Hi @johnpwarren :) I'm have a PHP webapp that needs to become a desktop app that has to work without internet connection. Unfortunately I can't migrate in NodeJS all the webapp so I am trying to make it work using Electron and 'gulp-connect-php' but I'm more interested in understand your proposal with Docker :D
Did you write a blog post or have further info to share??

@johnpwarren
Copy link

johnpwarren commented Sep 5, 2016

Just got it working as a low priority task for work a week ago, so too early to blog about it. Basically just dockerize your PHP app, in my case this was an official Nginx container, an official MariaDB container and a customized official PHP7-FPM container (on a Mac you'll want to sign up for and use the Docker native beta). Tie them together with docker-compose, enter a /etc/hosts entry for the web server locally, then just point the Electron renderer (usually main.js) to the hostname. It's really slow, rendering the same docker group in the browser is easily 5x faster than in Electron (not sure why yet), but seems to be fully functional. Packaging (that I have not gotten to yet) is going to be important as you'll have to have a version of docker and docker-compose verified or installed first and a host file entry working (still easier than keeping a native local LAMP stack installed). Customizing the dockers to run for a single desktop user is also going to be important as by default they each grab 2Gb of memory and way too many default procs for a desktop app, so I was running a very slow install of Drupal 7 in Electron at almost 7Gb of RAM with the mostly default containers. Drupal 8 seems to have an install bug with Nginx, but the plan is to use that with the Waterwheel project and Electron/Node for our app and Drupal/PHP will just be the backend for managing a ton of dynamic content.

@theBliz
Copy link

theBliz commented Sep 7, 2016

Thanks so much @johnpwarren :)That's a great idea, I'll try to make it work. If you blog about it let me know!

@johnpwarren
Copy link

@theBliz Just wanted to update, trimming down the resources in docker-compose.yml fixed the performance issues for me. I had pushed into my swap with the 7GB beast and everything else I had running at the time.

@sarabveer
Copy link

FYI, I got PHP Running on Electron using Gulp-Electron-php, will post a template on github soon.

@lakinmohapatra
Copy link

@sarabveer , Please post the tutorial . We are waiting for you.

@sarabveer
Copy link

@lakinmohapatra All I did honestly was follow that French Article Above.

@PrestaSafe
Copy link

Do you want i post it ??
(I'm the guy who created the french article)

@nnmrts
Copy link

nnmrts commented Oct 19, 2016

EDIT 2020-05-30:

i wrote this when i was 17 and inexperienced with electron and php. but i won't delete it so nothing breaks (but don't read it, don't use it, it's baragouin)

hey hey hey!

i finally found the easiest solution to do php stuff in electron

so basically, i think all php GET request are possible in electron, POST request actually not really

first of, you need to have node.js and npm (included in node.js), but i think you probably already have that

then, go to command line (as admin) and type:

cd C:\Program Files (x86)\nodejs

then:

npm install xhr

then go to C:\Program Files (x86)\nodejs\node_modules\xhr, and copy the index.js to your original web app directory (and rename it to xhr.js)

now include this inside the head tag of your index.html or index.php:
<script src="xhr.js"></script>

now, the complex part:

go to your main javascript file and convert all javascript, jquery or ajax post requests to xhr post requests by hand, just google the syntax of the xhr function for help with that or follow the example below

so you should just have post requests like this:

xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', '/path/to/some.php', true );
xhr.send(somedata);

this part is very important! you have to use xhr methods to get it working


so, that's the solution...no embedding, not much extra code, no gulp-php-stuff

just rebuild your app now and it should work


PS: answers like "doing this would make no sense, now learn a new language please" when someone asks for a easy workaround at least, are just bullshit...sorry for saying that, but as developers we all know that we first want to look for an easy way to do things with the resources we have...just ignoring the question and making the asker feel super bad for not wanting to rewrite his whole app, THAT makes no sense...php is just a language and i assume the right to use it like i want, damn

peace out

@sarabveer
Copy link

Wait what, how does that parse the PHP and send Data Back?

@sarabveer
Copy link

sarabveer commented Oct 19, 2016

So basically, you can't run PHP inside an asar file. I made a new folder called php in the resources folder, and then I dropped PHP for the OS I was building for.

Now add this to your main.js/app.js:

var path = require("path"),
    connect = require("gulp-connect-php"),
    con = connect.server();
function createWindow() {
    var con = connect.server({
        port: insertportnumhere,
        hostname: "127.0.0.1",
        base: path.resolve(process.resourcesPath) + "/php/extras/www", //<-- Where your PHP files are located/WWW root
        keepalive: false,
        open: false,
        bin: path.resolve(process.resourcesPath) + "/php/php", //<-- Path to PHP.exe or php bin file, leave as is for cross-platform suppport
        root: "/",
        stdio: "inherit"
    });
}

@nnmrts
Copy link

nnmrts commented Oct 19, 2016

@sarabveer
parsing:

xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'get', 'some.php', true );
response = xhr.responseText;
//do something with response
xhr.send(null);

for me even this works:

$.get("some.php", function(response) {
//do something with response
})

sending:

xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new activeXObject("Microsoft.XMLHTTP");
xhr.open( 'post', 'some.php', true );
xhr.send(data);

@sarabveer
Copy link

yea, but how is the php file being executed? @nnmrts

@lakinmohapatra
Copy link

@guillaume23 Please post the article .

@crownemmanuel
Copy link

you can use https://www.npmjs.com/package/gulp-connect-php to run php in electron
here is a working example
https://github.com/garak/electron-symfony

@crownemmanuel
Copy link

@embluk you can try this havnt used this personally myself
https://github.com/cztomczak/phpdesktop

@anggagewor
Copy link

@crownemmanuel
this is work for laravel too, I just tested with my laravel app.

screenshot_2017-05-10_01-39-13

@oleteacher
Copy link

@crownemmanuel That is cool! Been wanting to dive deeper into laravel. You by any chance have have app like you show on GitHub anywhere?

Thanks!

@aj-techsoul
Copy link

aj-techsoul commented Feb 7, 2018

You can # use php in electron, get a clone and enjoy php.
https://github.com/AJ-TechSoul/ELECTRON-4-PHP
electron4php

@ElioMS
Copy link

ElioMS commented Dec 20, 2018

@anggagewor Can you help with the configuration for laravel?

@MEGAMINDMK
Copy link

MEGAMINDMK commented Dec 18, 2019

You can use php in electron, get a clone and enjoy php.
https://github.com/MEGAMINDMK/PHP-Electronjs

@xiCO2k
Copy link

xiCO2k commented May 1, 2020

You can use php in electron, get a clone and enjoy php.
https://github.com/MEGAMINDMK/PHP-Electronjs

Does not support mac and linux, so not a good one

@MEGAMINDMK
Copy link

You can use php in electron, get a clone and enjoy php.
https://github.com/MEGAMINDMK/PHP-Electronjs

Does not support mac and linux, so not a good one

does support mac and linux get mac and linux binaries insert into it as it is and get the app working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests