DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Using Composer to Manage Dependenceis in Heroku PHP Apps

Using Composer to Manage Dependenceis in Heroku PHP Apps

Henri Bergius user avatar by
Henri Bergius
·
Jun. 12, 12 · Cloud Zone · Interview
Like (0)
Save
Tweet
6.41K Views

Join the DZone community and get the full member experience.

Join For Free

Heroku is a very nice Platform-as-a-Service provider that allows you to focus on writing applications instead of managing servers. If your application code is already managed in Git, in most cases you only need to create a Heroku app setup, and then git push to deploy it on Heroku. Scaling your app is easy and there are many useful add-ons available in their "app store".

While Heroku got its start from hosting Ruby on Rails applications, it nowadays supports many different environments in the Cedar stack. Node.js is what many use, but they also do support PHP.

Dependency management is easy for Node.js applications as Heroku recognizes your package.json files and automatically installs the libraries needed via NPM.

Until now PHP developers haven't had this convenience, but as Composer is emerging as the default PHP package manager, I've now added support for it. Before the pull request gets accepted, Composer dependency handling can already be used by specifying my custom PHP buildpack when creating Heroku apps.

I've written a simple example app to show how this works.

First you need to create the folder for your app and make it a Git repository:

$ mkdir myapp
$ cd myapp
$ git init

Then create the Heroku app using a custom buildpack (when the pull request is accepted you can skip the buildpack definition):

$ heroku create -s cedar --buildpack https://github.com/bergie/heroku-buildpack-php.git my-cool-app

Then it is time to write your composer.json file. In this case we'll only depend on the urlize library:

{
    "require": {
        "php": ">=5.2.0",
        "midgard/midgardmvc-helper-urlize": "*"
    }
}

For Heroku to recognize the app as a PHP one, you also need to have an index.php. In this case with the following code:

<?php
// URLizer service
require 'vendor/midgard/midgardmvc-helper-urlize/interface.php';
if (isset($_GET['urlize'])) {
    $data = array();
    $data['from'] = $_GET['urlize'];
    $data['to'] = midgardmvc_helper_urlize::string($_GET['urlize']);
    header('Content-type: application/json; charset=utf-8');
    die(json_encode($data));
}
header('Content-Type: text/html; charset=utf-8');
?>
<h1>Urlizer service</h1>
<form method="GET">
    <label>
        String to URLize
        <input name="urlize" type="text" />
    </label>
    <input type="submit" value="URLize" />
</form>

Now add and commit these files, and then deploy to Heroku:

$ git push heroku master

You should see that Heroku notices the Composer dependencies and installs them:

-----> Heroku receiving push
-----> Fetching custom buildpack... done
-----> PHP app detected
-----> Bundling Apache version 2.2.22
-----> Bundling PHP version 5.3.10
-----> Installing Composer dependencies
Installing dependencies
  - Package phptal/phptal (dev-master)
    Cloning e146361f25b8672d364695b757eddf1c169e05d2

  - Package midgard/midgardmvc-core (dev-master)
    Cloning 2b00d38cb2fea42c8f9791c5ecc7270dc81182e8

  - Package midgard/midgardmvc-helper-urlize (dev-master)
    Cloning 92d0c8c638c389b7be1887ca67cd334f51932912

midgard/midgardmvc-core suggests installing ext-midgard2 (>=10.05.5)
Writing lock file
Generating autoload files
-----> Discovering process types
       Procfile declares types -> (none)
       Default types for PHP   -> web
-----> Compiled slug size is 13.2MB
-----> Launching... done, v13

And that is it! You can see an example of this app at http://urlizer-service.herokuapp.com/.

PHP app Composer (software) Git

Published at DZone with permission of Henri Bergius, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Maven Tutorial: Nice and Easy [Video]
  • Modern REST API Design Principles and Rules
  • Memory Debugging and Watch Annotations
  • What SREs Can Learn From the Atlassian Nightmare Outage of 2022

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo