Subscribe: Planet PHP
Added By: Feedage Forager Feedage Grade B rated
Language: English
access control  access  control  design patterns  farm  headers  method  options  public function  request  return  server  web 
Rate this Feed
Rate this feedRate this feedRate this feedRate this feedRate this feed
Rate this feed 1 starRate this feed 2 starRate this feed 3 starRate this feed 4 starRate this feed 5 star

Comments (0)

Feed Details and Statistics Feed Statistics
Preview: Planet PHP

Planet PHP

People blogging about PHP


Let’s Compare: RunCloud vs Forge vs ServerPilot - SitePoint PHP

Sun, 24 Sep 2017 13:20:25 +0000

When your website or web application has outgrown your shared hosting account, it is time to move it to the cloud, or into a virtual private server (VPS). Nowadays, VPS providers offer better value in terms of CPU and RAM resources than the shared hosting counterparts. However, I can recall when I wanted to move my website to Amazon Web Services a few years ago, and it took me days to deploy the server and get it ready with PHP, MySQL and Apache. Fortunately, that is not the case today as we have the help of some cool tools and third party services. I took a look at three similar services - RunCloud, ServerPilot and Laravel Forge - and compared them to see which performs best. These are SaaS tools that can help deploy, configure, manage, and monitor VPS on any cloud hosting providers like AWS, Linode, Vultr, Digital Ocean, and others. Registration was easy. You don't need a credit card and you can even use your Facebook, Google, or GitHub account. If you don't fancy using your social media account, then just use your email address. With RunCloud, what you need to do is deploy a fresh Ubuntu 16.04 LTS VPS on almost any VPS hosting provider. Throughout this tutorial I used a Linode VPS which costs $5 per month. When your server is ready, you go back to RunCloud and enter your server details like the IP address to get your connection command. You will get a very long one-line command like this. All that you need to do is run the command in your server as the root user and let it run. It will probably take about 15 to 20 minutes for the process to complete, and once you are done, you can start to manage your server from within the web panel. Managing Your Server Next you need to create a Web Application. That is like a virtual host in the web server. When you do that, you can select your PHP version, and you can assign your domain name to the Web Application. You can assign more than one. The UI is very user friendly and intuitive and you can find whatever you need rather quickly and easily. My most basic need would be to be able to create virtual hosts (Web Application), assign domain names and create databases and perhaps deploy my application from GitHub or any other Git server. There is also a script installer that supports WordPress, Joomla, Concrete5, PHPMyAdmin and a few more common PHP tools. If you do PHP development and use Git, RunCloud supports deploying your script from GitHub, Bitbucket, Gitlab and also your own custom Git server installation. We'll look into performance later. ServerPilot is probably the oldest among the three, and most well-marketed. When I ask around if anybody is using a server management tool, it will probably be has a Free account that you can use with limited features, and there are also the Coach ($10/mo) and Business ($49/mo) plans. Please bear in mind, these prices are for server deployments and not per account to ServerPilot. Coach has more features like free SSL via LetsEncrypt, server health stats and other. The Business package is more like a mini NewRelic or an application performance monitoring service. You can see slow script request, application resources stats and more. Of course the Free package is good enough, but limited compared to their Coach and Business options. Connecting A Server This feels similar to RunCloud. You also need a fresh Ubuntu 16.04 VPS server to start. If you have the root password to your server, you don't need to copy and paste the installer command via SSH. You can enter your server IP address and your root password, and ServerPilot will get your server ready in a few minutes. However, if you don't want to enter the root password of your server into ServerPilot, then you can opt not to do that. You can get iTruncated by Planet PHP, read more at the original (another 5024 bytes)[...]

Implementing Serverless PHP - Nomad PHP

Fri, 22 Sep 2017 04:01:54 +0000

December - EU
Presented By

Rob Allen
December 21, 2017
20:00 CET

The post Implementing Serverless PHP appeared first on Nomad PHP.

CORS and OpenWhisk web actions - Rob Allen

Wed, 20 Sep 2017 10:03:00 +0000

By default, OpenWhisk will handle the relevant responses for CORS. Specifically, it will respond to an OPTION request with these headers: Access-Control-Allow-Origin: * Access-Control-Allow-Methods: OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH Access-Control-Allow-Headers: Authorization, Content-Type If you need to change what is sent or you don't want to send these headers at all, then you need to do set the annotation web-custom-options to true and handle the OPTIONS header yourself. Note that if you don't set this annotation, you must not set any of these headers yourself as you'll break things! Implementing the CORS headers yourself To implement the CORS headers yourself you do something like this in your action (example in Swift, PHP version at the end): func main(args: [String:Any]) -> [String:Any] { let corsAllowedOrigin = "" // URI that may access this resource ("*" is wildcard) // OPTIONS handling for CORS guard let method = args["__ow_method"] as? String, let headers = args["__ow_headers"] as? [String:Any] else { return ["statusCode": 500] } if method == "options" { let allowedHeaders = headers["access-control-request-headers"] ?? "Content-Type, Authorization" return [ "statusCode": 200, "headers": [ "Access-Control-Allow-Methods": "OPTIONS, GET, PUT, DELETE", "Access-Control-Allow-Origin": corsAllowedOrigin, "Access-Control-Allow-Headers": allowedHeaders ] ] } let data = ["hello": "world"] return [ "statusCode": 200, "headers": [ "Content-Type": "application/json", "Access-Control-Allow-Origin": corsAllowedOrigin, // don't forget this header! ], "body": Data(WhiskJsonUtils.dictionaryToJsonString(jsonDict: data)!.utf8).base64EncodedString() ] } Create it it OpenWhisk with: wsk action create api api.swift --web true -a web-custom-options true For a "non-simple" request, the browser will send an OPTIONS request to the option that you need to respond to. This is call the "preflight" request. The __ow_method in args will tell you the method (lowercased) and __ow_headers will contain all the headers that were sent. If the method is options, then we need to return the CORS headers. You can expect that the client will send a Access-Control-Request-Headers header containing the list of headers that its going to send to you when it does the actual request. If you don't have a white list of headers that you're expecting, then you should send back the list you are sent as otherwise the request will fail. You also need to set the Access-Control-Allow-Methods header to the list the methods you accept for this end point; it is the CORS version of the Allow header. The client should send you a Access-Control-Request-Method header to tell you the method it will use when it sends the actual request. If the method is not OPTIONS, then you send back your response as usual. In addition, you need to send the Access-Control-Allow-Origin header with the same value as you used for the preflight request. That's it. Aside: PHP version As an aside, here's how it looks in PHP, where the type handling looser: 200, "headers" => [ "Access-Control-Allow-Methods" => "OPTIONS, GET, PUT, DELETE", "Access-Control-Allow-Origin" => $corsAllowedOrigin, "Access-Control-A[...]

“Before” (not “Beyond”) Design Patterns - Paul M. Jones

Tue, 19 Sep 2017 12:00:51 +0000

(N.b.: This has been languishing at the bottom of my blog heap for years; time to get it into the sun.)

The 2013 article Beyond Design Patterns takes the approach of reducing all design patterns to a single pattern: “Abstracting Communication Between ‘Components’.” The author concludes, in part:

Instead of focusing on design patterns, I would suggest focusing on understanding how communication between objects and components happens. How does an object “decide” what to do? How does it communicate that intention to other objects.

Are design patterns useful? Absolutely. But I’ll assert that once you understand OOP and object communication, the patterns will “fall out” of the code you write. They come from writing OOP.

This strikes me as misapplied reduction. Sure, it’s true that, at a lower level or scope, it might be fair to say that “the pattern is ‘communication.’” But it is also true that, at a somewhat higher level or scope, communication between “which things” in “what way” is also fair. So the article might better be titled not “Beyond Design Patterns” but “Beneath Design Patterns” or “Before Design Patterns”. The concepts illustrated are not consequences of or improvements on design patterns; they are priors to design patterns.

The analogy that came to my mind was one of molecules and atoms. An imaginary article on chemistry titled “Beyond Molecules” might thus conclude …

Instead of focusing on molecules, I would suggest focusing on understanding how interaction between atoms happens. How does an atom “decide” what to do? How does it communicate that intention to other atoms?

Are molecules useful? Absolutely. But I’ll assert that once you understand atoms and atomic interaction, the molecules will “fall out” of the formulas you write.

… which is true enough for as far as it goes, but it is also revealed as a rather superficial and mundane observation when presented this way. Atoms are not “beyond” molecules.

So: molecules are a proper unit of understanding at their level or scope. Likewise, design patterns are a proper unit of understanding at their own level. Reducing them further does not show you anything “beyond” – it only shows you “because.”

Laravel QueryDumper - Sarfraz Ahmed

Sat, 16 Sep 2017 13:16:00 +0000

I wanted simple and quick way to see all running queries on any page of my laravel application especially accompanied by MySQL's EXPLAIN results. So I created very simple package called Laravel QueryDumper that allows me see just that.



Checkout Laravel QueryDumper


Procedurally Generated Game Terrain with React, PHP, and WebSockets - SitePoint PHP

Thu, 14 Sep 2017 17:00:01 +0000

Last time, I began telling you the story of how I wanted to make a game. I described how I set up the async PHP server, the Laravel Mix build chain, the React front end, and WebSockets connecting all this together. Now, let me tell you about what happened when I starting building the game mechanics with this mix of React, PHP, and WebSockets… The code for this part can be found at I've tested it with PHP 7.1, in a recent version of Google Chrome. Making a Farm "Let's start simple. We have a 10 by 10 grid of tiles, filled with randomly generated stuff." I decided to represent the farm as a Farm, and each tile as a Patch. From app/Model/FarmModel.pre: namespace App\Model; class Farm { private $width { get { return $this->width; } } private $height { get { return $this->height; } } public function __construct(int $width = 10, int $height = 10) { $this->width = $width; $this->height = $height; } } I thought it would be a fun time to try out the class accessors macro by declaring private properties with public getters. For this I had to install pre/class-accessors (via composer require). I then changed the socket code to allow for new farms to be created on request. From app/Socket/GameSocket.pre: namespace App\Socket; use Aerys\Request; use Aerys\Response; use Aerys\Websocket; use Aerys\Websocket\Endpoint; use Aerys\Websocket\Message; use App\Model\FarmModel; class GameSocket implements Websocket { private $farms = []; public function onData(int $clientId, Message $message) { $body = yield $message; if ($body === "new-farm") { $farm = new FarmModel(); $payload = json_encode([ "farm" => [ "width" => $farm->width, "height" => $farm->height, ], ]); yield $this->endpoint->send( $payload, $clientId ); $this->farms[$clientId] = $farm; } } public function onClose(int $clientId, int $code, string $reason) { unset($this->connections[$clientId]); unset($this->farms[$clientId]); } // … } I noticed how similar this GameSocket was to the previous one I had --- except, instead of broadcasting an echo, I was checking for new-farm and sending a message back only to the client that had asked. "Perhaps it's a good time to get less generic with the React code. I'm going to rename component.jsx to farm.jsx." From assets/js/farm.jsx: import React from "react" class Farm extends React.Component { componentWillMount() { this.socket = new WebSocket( "ws://" ) this.socket.addEventListener( "message", this.onMessage ) // DEBUG this.socket.addEventListener("open", () => { this.socket.send("new-farm") }) } } export default Farm In fact, the only other thing I changed was sending new-farm instead of hello world. Everything else was the same. I did have to change the app.jsx code though. From assets/js/app.jsx: import React from "react" import ReactDOM from "react-dom" import Farm from "./farm" ReactDOM.render( , document.querySelector(".app") ) It was far from where I needed to be, but using these changes I could see the class accessors in action, as well as prototype a kind of request/response pattern for future WebSocket interactions. I opened the console, and saw {"farm":{"width":10,"height":10}}. "Great!" Then I created a Patch class to represent each tile. I figured this was where a lot of the game's logic would happen. From app/Model/PatchModel.pre: namespace App\Model; class PatchModel { private $x { get { return $this->x; } } private $y { get { return $this->y; } } public function __construct(int $Truncated by Planet PHP, read more at the original (another 9046 bytes)[...]

Symfony Console Beyond the Basics – Helpers and Other Tools - SitePoint PHP

Thu, 14 Sep 2017 06:00:47 +0000

It's undeniable how useful console commands can be when developing software. Not too long ago we re-introduced the Symfony Console component. This component allows us to create structured and testable CLI commands. We created some simple commands and tested them; but when our commands become bigger and more complex, we need a different set of tools. This is what we are going to look at today: advanced Symfony console tools. Let's create a command that we can use to show some of these features. Most of the basic functionality was shown in the re-introduction to the Symfony console article, so be sure to check it before advancing - it's a quick but useful read! Installation composer require symfony/console Essential information about Composer can be found here, and if you're not familiar with well designed isolated PHP environments in which to develop your PHP apps like Vagrant, we have a fantastic book explaining it all in depth available for purchase here. Creating our command Let's create a command for an all time favorite: Fizzbuzz. Fizzbuzz is a simple problem often used in programming interviews to assert the programming competence of the interviewee. The definition of Fizzbuzz normally comes in the following form: Write a program that prints the numbers from 1 to x. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz". Our command will receive an argument which will be the top limit for Fizzbuzz. First of all, let's create our Fizzbuzz class. isFizz($number) && $this->isBuzz($number)){ echo "FizzBuzz \n"; return true; } if($this->isFizz($number)){ echo "Fizz \n"; return true; } if($this->isBuzz($number)){ echo "Buzz \n"; return true; } echo $number . "\n"; return true; } public function firstNFizzbuzz(int $maxValue): void{ $startValue = 1; while($startValue calculateFizzBuzz($startValue); $startValue++; } } } Pretty straightforward. The firstNFizzbuzz() method prints the results of Fizzbuzz for a $maxValue of numbers. It does this by calling the calculateFizzBuzz() method recursively. Next, let's write our command. Create a FizzCommand.php file with the following contents: Continue reading %Symfony Console Beyond the Basics – Helpers and Other Tools% [...]