スキップしてメイン コンテンツに移動

投稿

ラベル(PHP)が付いた投稿を表示しています

Symfony2: Switching URL for Different Environment

Issue For Symfony2, if you would like to setup different url based on environment but using same action in controller, what should we do? In my case, I would have liked to setup different url based on different country enviroment but would have liked to use same action becaseu the functionality itself is completely same. Solution Setup routing.yml for each environment. Symfony2 porvides mailnly 2 ways to setup routing configuration - routing.yml and annotation on controller. I think using annotation is better way because you can easily check functionality and routing relation in controller class. However if you would like to setup different url based on different environment, using routing.yml is better way. Ok now I will show you my approach.... 1) You should prepare routing_shared.yml for common shared routing between environment. 2) You should preare routing.yml for rach enviroment which is depends on the environment, like routing_env_1.yml, routing_env_2.yml, etc. And

Get Youtube Information by PHP cURL

I have written small code snippet for getting information from Youtube by PHP with cURL! The code itself is nothing special, really straightforward manner. The following getVideoInfo function takes youtube video id, access to youtube api and return back the detail video information. function getVideoInfo($id) { try { $req = request('http://gdata.youtube.com/feeds/api/videos/' . $id); $xmlStr = $req->getResponseData(); $xml = simplexml_load_string($xmlStr); $xml->registerXPathNamespace('x', 'http://www.w3.org/2005/Atom'); $xml->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/'); $xml->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007'); $title = $xml->xpath('/x:entry/media:group/media:title'); $duration_seconds = $xml->xpath('/x:entry/media:group/yt:duration/@seconds'); if (empty($title

Solve Local Capistrano Deployment Issue

Local Capistrano Deployment Issue I have an inssue when deploying PHP application on "local" machine (e.g. deploying app to same machine where executing capistrano deploy.) using Capistrano. The cause is when capistrano creates temporary directory for destination (remote) directory and directory for source (local) directory point to same directory. This problem does not happen when you deploy to different machine. Solution Add the below code to your deploy.rb!! Just change the name for temoprary directory used during deployment name. # put this line at the top of app.yml in order to use SecureRandom methods require "securerandom" # set flag true when local deployment set :deploy_to_self, "true" # hook tasks before :deploy, "local:create_dir" after :deploy, "local:clean_dir" #################################### # setup copy_dir, copy_remote_dir # when deploying to machine which this script is running on to avoid copy issue # na

Restrict Html Tags which User Can Input (PHP)

I tried to implement very simple html edit text area which has available tags user can input are restricted. So I needed to implement a validator which detects tags not allowed to use. The proper (but a bit heavy) implementation approach is using Tidy . It can validate entire html and also fix and clean up html source! However in my case using tidy is a bit overkill solution. Instead of using tidy, I decided to use strip_tags function. The disadvantage is that the function does not validate html syntax. e.g. inaccurate than using tidy.- "strip_tags function does not actually validate the html, partial or broken tags can result in the removal of more text/data than expected." as the official PHP document says. Okie, as long as we understand the disadvantage, we can use this function. Let's show you the code. function validateOnlyAllowedTags($html, $tags) { $stripped = strip_tags($html, $tags); // if no tags are stripped, the length of html contents

Symfony 1.4 Accessing Context

Symfony 1.4 is already a legacy framework. But I think a lot of web system still running on it. In this post, I will show you some quick tips for accessing infromation by using sfContext . sfContext provides almost everything you want access. But you should not use context everywhere too much because sfContext is a kind of global objeect and if a lot of codes depends on context, your code will be hard to be tested or maintained, I think. Basic You can access sfContext by following code. sfContext::getInstance(); In sfFilter, you can access sfContext by the following code. $this->getContext(); What Kind of Fields You can access? Routing name $context->getRouting()->getCurrentRouteName(); User $context->getUser(); Request $context->getRequest(); Response $context->getResponse(); Controller $context->getController(); Logger sfContext::getInstance()->getLogger(); Advanced Use Check if http request is secure (e.g. https request). $co

PHP: Extract Values on Specific Key in Array

I wrote tiny utility method - which is for extracting values on specific key in array. Code function extractValuesForSpecificKey(array& $sourceArray, $key){ $retArray = array(); foreach ($sourceArray as $sourceElem){ $retArray[] = $sourceElem[$key]; } return $retArray; } Example $source = array( array('id' => 1, 'name' =>'Joe'), array('id' => 2, 'name' =>'Chris'), ); $result = extractValuesForSpecificKey($source, 'id'); var_dump($result); The result should be: array(2) { [0]=> int(1) [1]=> int(2) }

Phalanger

I didn't know that there is a PHP compiler - Phalanger It compiles PHP on .Net/Mono platform. The aim of Phalanger is improving performance of PHP  Web application. I am surprised that PHP compiled by Phalanger is four times faster than offical PHP 5.4. Will try it when I have a free time.

PHP: Grouping Elements in Array by Specific Key Field

This is the code for grouping elements in array by specific key field of the element. Code <?php function groupBySpecificKey(array& $source, $key){ $map = array(); foreach($source as $elem){ $groupKey = $elem[$key]; if(is_null($groupKey)) continue; $map[$groupKey][] = $elem; } return $map; } Example $result = groupBySpecificKey($source, 'country'); var_dump($result); $source = array( array('id' => 1, 'name' =>'Joe', 'country' => 'China'), array('id' => 2, 'name' =>'Chris', 'country' => 'USA'), array('id' => 3, 'name' =>'Tod', 'country' => 'USA'), ); The result is below. array(2) { ["China"]=> array(1) { [0]=> array(3) { ["id"]=> int(1) ["name"]=> string(3) "Joe"

クラウド関連のメモ

プラットフォーム Heroku サポート言語数の高いウェブホスティングサービス。サイトには"Ruby, Node.js, Clojure, Java, Python, and Scala. "と書かれているので、かなりの言語が自由に使えるようです。機会があればぜひ使ってみたいです。 AppFog  特に PHPFog はHerokuではサポートされていないPHPのアプリケーションの短期開発に有用そうです。 Windows Azure Web API Windows Live API

PHP: Immutable Row Update by Delete and Insert Operation

If you would like to realize update rows by only allowing delete and insert rows on DB, set operation might be helpful. For PHP, using array_diff function is really useful for realizing the operation. // we would like to insert '4', '5' and delete '2' in this example. // how to do this? $original_ids = array('1', '2', '3'); $new_ids = array('1', '3', '4', '5'); // one solution // 1) the key point is calculating subtract set. // 2) array_values is used only for re-numbering index. e.g. all index will be 0 origin sequence . // the result will be array(1){ [0] => '4', [0] => '5' } $ids_insert = array_values(array_diff($new_ids, $original_ids)); // the result will be array(1){ [0] => '2' } $ids_delete = array_values(array_diff($new_ids, $original_ids));