{"id":17482912,"url":"https://github.com/creationix/rack-php","last_synced_at":"2025-04-10T02:43:39.101Z","repository":{"id":66014254,"uuid":"245853","full_name":"creationix/rack-php","owner":"creationix","description":"A php 5.3 framework with built in rack support.  You can serve the php site using thin, mongrel, webrick, passenger, mod_php, or mod_fcgid.","archived":false,"fork":false,"pushed_at":"2009-12-15T04:16:51.000Z","size":202,"stargazers_count":50,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T04:14:01.147Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/creationix.png","metadata":{"files":{"readme":"README.markdown","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2009-07-08T04:18:23.000Z","updated_at":"2024-09-21T18:16:58.000Z","dependencies_parsed_at":"2023-02-19T21:10:14.675Z","dependency_job_id":null,"html_url":"https://github.com/creationix/rack-php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creationix%2Frack-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creationix%2Frack-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creationix%2Frack-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/creationix%2Frack-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/creationix","download_url":"https://codeload.github.com/creationix/rack-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248145348,"owners_count":21055112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-18T23:47:44.247Z","updated_at":"2025-04-10T02:43:39.076Z","avatar_url":"https://github.com/creationix.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rack-php\n\nRack-php is a PHP 5.3 framework that adheres to the rack interface from the ruby world (\u003chttp://rack.rubyforge.org/\u003e).\n\nIt includes a simple php framework that lightly based around mvc.  Instead of using php's built in $_SERVER variables, various adaptors are used.\n\nThere is an adaptor for regular php web hosting (mod_php, mod_fcgid, etc...).  These are the current methods of serving php sites.  The adaptor will transform these environments into a rack request and load your rack-php app.\n\nAlso is an adaptor that bridges ruby and php allowing php rack apps to run as a rack endpoint to regular ruby rack servers.  This means you can run your php app using cool things like thin, mongrel, webrick, and passenger.\n\n## Rackup Files\n\n\nHere is a simple rackup.php file.  This is the php script bootstrapped by the various rack servers.\n\n\n**public/index.php** \u003chttp://cloud.github.com/downloads/creationix/rack-php/apache.png\u003e\n\n    \u003c?\n    require 'autoload.php'; // Set up class auto-loading\n\n    $app = new SampleApplication();\n    $env = \\core\\Rack::run($app);\n\nThis version is meant to be the index.php file used by traditional web hosts (mod_php, mod_fcgid, etc...)\n\n**rackup.php** \u003chttp://cloud.github.com/downloads/creationix/rack-php/thin.png\u003e\n\n    #!/usr/bin/env php\n    \u003c?\n    require 'autoload.php'; // Set up class auto-loading\n\n    $app = new SampleApplication();\n    $env = \\core\\RubyBridge::run($app);\n\nAs you can see, this is a simple command line php script.  It creates an instance of our application and then runs it through the RubyBridge adaptor.\n\n### Ruby server\n\nThis script in turn is called by the ruby config.ru file that follows:\n\n**config.ru**\n\n    require 'json'\n\n    class RubyBridge\n      def call(env)\n        response = []\n        data = JSON.dump(env)\n        IO.popen(\"./rackup.php\", 'r+') do |io|\n          io.write data\n          io.close_write\n          response = io.read\n        end\n        JSON.load(response)\n      end\n    end\n\n    run Cascade.new([\n      File.new('public'),\n      RubyBridge.new\n    ])\n\nRubyBridge is simple a proxy class that passes the real work to the php script via process pipes.\n\nHere we can take advantage of the ruby Rack::File middle-ware to serve static files.  This is needed when running through servers like thin or mongrel directly.\n\nWe can call PHP middleware through a RubyMiddlewareBridge class:\n\n**config.ru**\n    \n    class RubyMiddlewareBridge\n      def initialize(app)\n        @app = app\n      end\n\n      def call(env)\n        env['rack.ruby_bridge_response'] = @app.call(env)\n\n        response = []\n        data = JSON.dump(env)\n        IO.popen(\"./rackup.php\", 'r+') do |io|\n          io.write data\n          io.close_write\n          response = io.read\n        end\n        JSON.load(response)\n      end\n    end\n\n    use RubyMiddlewareBridge\n    use Rack::Reloader\n    use Rack::ContentLength\n\n    app = proc do |env|\n      [200, {'Content-Type' =\u003e 'text/html'}, ['Hello world']]\n    end\n\n    run app\n\nAnd inside the rackup.php we can use PHP middleware to continue the rack app callchain:\n\n**rackup.php**\n\n    #!/usr/bin/env php\n    \u003c?\n    require realpath(dirname(__FILE__).'/lib/autoload.php');\n    use core\\RubyBridge;\n\n    class SwapHelloWorld extends core\\App\n    {\n      protected $app;\n\n      public function __construct($app)\n      {\n        $this-\u003eapp = $app;\n      }\n\n      public function __invoke($env)\n      {\n        list($status, $headers, $body) = $this-\u003ecall($this-\u003eapp, $env);\n        $parts = split(' ', $body[0]);\n        $body = $parts[1] . ' ' . $parts[0];\n        return array($status, $headers, array($body));\n      }\n    }\n\n    RubyBridge::useMiddleware('SwapHelloWorld');\n    RubyBridge::run(); // =\u003e world Hello\n\n## Simple Rack PHP App\n\nHere is a small sample rack-php app that simply ignores all input and outputs the string \"Hello World\".\n\n    class SampleApplication extends App\n    {\n      public function __invoke($env)\n      {\n        return array(200, array(\"Content-Type\"=\u003e\"text/plain\"), array(\"Hello World\"));\n      }\n    }\n\nAs you can see, this works just like a ruby rack app.  The __invoke magic function is passed the environment hash, and it must return an array of status, headers, body.\n\nIn ruby 1.9 and my php adaptor, the body needs to be iterable, so the body string needs to be wrapped in an array.\n\nHere is an example how to use a Rack PHP App with middleware:\n\n**public/index.php**\n\n    \u003c?php\n    require realpath(dirname(__FILE__).'/../lib/autoload.php');\n    use core\\Rack;\n    use core\\App;\n\n    class UpcaseMiddleware extends App\n    {\n      protected $app;\n\n      public function __construct($app)\n      {\n        $this-\u003eapp = $app;\n      }\n\n      public function __invoke($env)\n      {\n        list($status, $headers, $body) = $this-\u003ecall($this-\u003eapp, $env);\n\n        $upcase_body = strtoupper($body[0]);\n        return array($status, $headers, array($upcase_body));\n      }\n    }\n\n    Rack::useMiddleware('UpcaseMiddleware');\n\n    // We can also use the lambda function\n    $app = function ($env) {\n      return array(200, array('Content-Type' =\u003e 'text/html'), array('Hello World!'));\n    };\n\n    Rack::run($app); // =\u003e HELLO WORLD!\n\n## TODO:\n\n  * Implement file upload\n  * Error Log Logging\n  * Find ways to better follow the spec\n  * Make a cool site using the framework\n  * php middle-ware inside of a rack stack (not sure this is possible)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreationix%2Frack-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcreationix%2Frack-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcreationix%2Frack-php/lists"}