{"id":142,"date":"2014-12-17T22:26:36","date_gmt":"2014-12-18T04:26:36","guid":{"rendered":"http:\/\/jacobncalvert.com\/?p=142"},"modified":"2018-02-10T15:26:05","modified_gmt":"2018-02-10T21:26:05","slug":"introducing-webphp-another-php-framework","status":"publish","type":"post","link":"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/","title":{"rendered":"Introducing WebPHP, a(nother) PHP framework"},"content":{"rendered":"<p>I&#8217;ve been working on a new project lately and I&#8217;m calling it WebPHP. I wanted something that operated somewhat like Tornado for Python.. but in PHP.<\/p>\n<p>I essentially wanted some framework that could simply be dropped onto the root of a hosted server from GoDaddy or the like, and a developer could immediately map URLs using a regex-like syntax to handler classes. I found some other frameworks to do this, but a lot of them seemed tightly coupled to a front-end framework not to mention complicated to install or maintain. Hence my creation of WebPHP. WebPHP works by mapping a URL with regex style syntax like<\/p>\n<pre class=\"prettyprint\">\t\/api\/users\/(.+)\/?\t\t\tMaps to http:\/\/example.com\/api\/users\/{anything}\r\n\r\n\t\/api\/users\/jacobcalvert\/profile\/?\tWould get my profile in this example service\r\n<\/pre>\n<p>to some handler class that would do something with the request. The way this map is defined is shown below:<code><\/code><\/p>\n<pre class=\"prettyprint\">\tnew webphp\\UrlMap(\"\/(.+)\/?\", array(\"the_variable\"),'ExampleHandler', array())\r\n<\/pre>\n<p>WebPHP would populate a variable called &#8220;the_variable&#8221; and instantiate an object of type ExampleHandler. ExampleHandler must be a derived class of the base WebRequest class defined by WebPHP. ExampleHandler could look like:<\/p>\n<pre class=\"prettyprint\">class ExampleHandler extends webphp\\WebRequest\r\n{\r\n    \/*\r\n     * example handler for webphp\r\n     *\/\r\n    public function get()\r\n    {\r\n        $this-&gt;add_header(\"Content-Type\", \"application\/json\");\r\n        $this-&gt;write(json_encode($this-&gt;params, JSON_UNESCAPED_SLASHES)); \r\n    }\r\n    \r\n    public function post()\r\n    {\r\n        $this-&gt;add_header(\"Content-Type\", \"application\/json\");\r\n        $this-&gt;set_response_code(201); \/\/ 201 is the \"CREATED\" response -- could be used for REST impl's\r\n        $this-&gt;write(json_encode($this-&gt;params, JSON_UNESCAPED_SLASHES)); \r\n    }\r\n    \r\n    public function put()\r\n    {\r\n        $this-&gt;add_header(\"Content-Type\", \"application\/json\");\r\n        $this-&gt;write(json_encode($this-&gt;params, JSON_UNESCAPED_SLASHES)); \r\n    }\r\n    \r\n    public function delete()\r\n    {\r\n        $this-&gt;set_response_code(403);\r\n        $this-&gt;write(NULL);        \r\n    }\r\n    \r\n    public function head()\r\n    {\r\n        \/\/ just an example of adding a custom header\r\n        \/\/ the HEAD verb expects just the headers back about \r\n        \/\/ the requested document\r\n        \/\/ or in other words, HEAD wants a GET response without the body\r\n        $this-&gt;add_header(\"Custom-Header\", \"custom-value\");\r\n        $this-&gt;write(NULL);\r\n    }\r\n}\r\n<\/pre>\n<p>The WebPHP app would call the appropriate handler method depending on what HTTP verb was used in the request. If no method is defined for a verb that is used, the default return is a 501\/Not Implemented response. One of the interesting features I was able to add is the ability to enforce SSL connections on specific URL maps or on entire handlers. Hop over to the\u00a0<a href=\"https:\/\/github.com\/jacobcalvert\/webphp\">GitHub page\u00a0<\/a>for more information and to see all the classes and example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been working on a new project lately and I&#8217;m calling it WebPHP. I wanted something that operated somewhat like Tornado for Python.. but in PHP. I essentially wanted some framework that could simply be dropped onto the root of a hosted server from GoDaddy or the like, and a developer could immediately map URLs using a regex-like syntax to handler classes. I found some other frameworks to do this, but a lot of them seemed tightly coupled to a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[56,54,53,55],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Introducing WebPHP, a(nother) PHP framework - Jacob N Calvert<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Introducing WebPHP, a(nother) PHP framework - Jacob N Calvert\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been working on a new project lately and I&#8217;m calling it WebPHP. I wanted something that operated somewhat like Tornado for Python.. but in PHP. I essentially wanted some framework that could simply be dropped onto the root of a hosted server from GoDaddy or the like, and a developer could immediately map URLs using a regex-like syntax to handler classes. I found some other frameworks to do this, but a lot of them seemed tightly coupled to a&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/\" \/>\n<meta property=\"og:site_name\" content=\"Jacob N Calvert\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-18T04:26:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-02-10T21:26:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#website\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"name\":\"Jacob N Calvert\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/jacobncalvert.com\/blog-archive\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/#webpage\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/\",\"name\":\"Introducing WebPHP, a(nother) PHP framework - Jacob N Calvert\",\"isPartOf\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#website\"},\"datePublished\":\"2014-12-18T04:26:36+00:00\",\"dateModified\":\"2018-02-10T21:26:05+00:00\",\"author\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#\/schema\/person\/f4b22a996d41bf09ed2bbe22912a8c8a\"},\"breadcrumb\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/2014\/12\/17\/introducing-webphp-another-php-framework\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#\/schema\/person\/f4b22a996d41bf09ed2bbe22912a8c8a\",\"name\":\"Jacob\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/jacobncalvert.com\/blog-archive\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/64a2dd1c00cb39dfc19bb1204c87efbc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/64a2dd1c00cb39dfc19bb1204c87efbc?s=96&d=mm&r=g\",\"caption\":\"Jacob\"},\"sameAs\":[\"https:\/\/jacobncalvert.com\"],\"url\":\"https:\/\/jacobncalvert.com\/blog-archive\/author\/jcalvert\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/142"}],"collection":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":1,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/posts\/142\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jacobncalvert.com\/blog-archive\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}