Yesterday I wrote about Standalone Forms and Validation. It may need
a little bit knowledge on how the Aura.Filter works. But that is
a good start if you want to know how you can integrate the pieces of Aura.
Today I think I need to show you the very minimal approach you can take
to build the form and validate your form. If you want to
use a powerful validation and filtering use something like Aura.Filter
or bind your favourite components which does the validation Respect, Symfony2 Validator,
valitron and filtering with something like DMS-Filter.
You can see how you can integrate with different components as I did with
Aura.Filter in the example in master branch.
I assume you know a bit about composer and how to install.
The Aura.Input itself contains a basic filter implementation. As shown
in earlier post Aura Turns 2
But in this post let us use the power of Aura.Filter. As Aura.Input
doesn’t have a rendering capability you may need to use Aura.View as
templating system ( see Using Aura.View ) or use the helper classes provided by Aura.View
( see below Without using Aura.View completely )
or create your own helper classes to render the same from the hints
( see Hints for the view ).
It has a file hell.php. I have created it by logging into user X
and with vim as the editor without any settings in .vimrc.
Then from user harikt I have edited with vim which has some .vimrc
setting like 1 tab = 4 spaces. Look into the code and see how beautiful
it is.
Opening with geany it looks
Let us don’t create problems, but solutions!
It is just one line you need to mention
“According to your editor settings set 1 tab = 4 spaces.” . no?
Or you don’t need to convert the tab at all?
Is your tabs 4 spaces, 8 spaces, 2 spaces?
Please fork and commit with your favourite editor mentioning the settings,
editor in your commit.
As as user I prefer consistency . And as I work with Aura, Symfony2,
ZF1, ZF2 I love to follow spaces, than individual projects choosing their
own styles which is a main benefit probably mostly to its users like me
than the core developers of individual system who don’t care about
other projects or its users?
After all when your project says we use tabs, do you care people who love
to use spaces there? Do you merge when something like this comes?
And today, I would like to introduce you, the new born baby still under active
development and refactoring based on user feedback, the
form library for php, Aura.Input.
The Aura.Input, doesn’t have a rendering functionality. But you can always
use Aura.View or create your own helpers.
A basic filtering based on closure exists in Aura.Input. But you are
not limited, you can use your own filtering components or
integrate Aura.Filter.
<?php// use composer or require '/path/to/Aura.Input/src.php';useAura\Input\Form;useAura\Input\Builder;useAura\Input\Filter;$filter=newAura\Input\Filter();// validate$filter->setRule('name','Name should be alpha only',function($value){returnctype_alpha($value);});$filter->setRule('email','Enter a valid email address',function($value){returnfilter_var($value,FILTER_VALIDATE_EMAIL);});$filter->setRule('url','Enter a valid url',function($value){returnfilter_var($value,FILTER_VALIDATE_URL);});$filter->setRule('message','Message should be more than 7 characters',function($value){if(strlen($value)>7){returntrue;}returnfalse;});classContactFormextendsForm{publicfunctioninit(){$name=$this->setField('name');$email=$this->setField('email');$url=$this->setField('url');$message=$this->setField('message','textarea');}}$form=newContactForm(newBuilder,$filter);$values=['name'=>'Hari K T','email'=>'oh will it works!','url'=>'google.com','message'=>'Aweso'];$form->setValues($values);$passed=$form->filter();// 'foo' is invalidif(!$passed){// get all messages$actual=$form->getMessages();var_dump($actual);}
Try it out, we have some more documentation for the Aura.Input. I warn
the api is still not stable for Aura.Input and is not yet released a Beta.
What it gives you is an object of Aura\Http\Manager. If you want to create
manually you can look into the instance.php
Building your Response
Probably you may not have bothered too much on building the http response
either the framework does it for you, or until you need to send the correct
response.
To create a proper http response via Aura.Http we need to create a
response object.
1
$response = $http->newResponse();
Now you have the response object. You can set the header via
1
$response->headers->set('Header', 'Value');
If you have an array of headers you can use setAll
12345678
$response->headers->setAll([ 'Header-One' => 'header one value', 'Header-Two' => [ 'header two value A', 'header two value B', 'header two value C', ],]);
You can get a cookie by calling get method on cookies.
1
$response->cookies->get('cookie_name');
Setting and Getting Status
By default the status code is 200. But at some point of time
like the one I explained earlier in Status Code 304, we don’t need to
send the whole content. But just the status code.
This is possible via setStatusCode and setStatusText
12
$response->setStatusCode(304);$response->setStatusText('Same As It Ever Was');
Sending your response
And finally we can send the response back. We can call the send
method and pass the response object.
You would have noticed I have used $_SERVER variable. In Aura.Http,
there is no methods to access the global server values. This is because
the $_SERVER values are not the exact http requested header. The server
modifies the request and we will be only getting the manipulated values
if we use $_SERVER, $_GET, $_POST values.
Aura.Http only helps you to build, create, modify response and request.
Creating Http Request
We talked about response so far. What does Request actually mean?
Client -> Request something -> Server Responds
So that means we are trying to be a client or a browser, and making the necessary
headers and sending to server to get the corresponding response.
We can get all the repos of a user in github via curl.
The same can be achieved via Aura.Http. The Aura.Http provides a
means to do the same with PHP. It uses curl if it is available or stream
to make this happen.
Recently github introduces contributions chart. So I peeped into some of the people of our php community. It seems @fabpot is the one who contributes almost every day. Just 21 days without any contribution.
and I too did some contributions in 2012 for auraphp and other projects.
The list is not complete, if you feel I missed your name or someone you feel needs to be added just give a tweet to me or fork the repo from github and send a PR.
Comparing a software X with Y will not make anything worst, but makes it better. But many of them will think it as a promotional stuff. It is not anyones’s problem. Some like to build it that way, some like the other way.
So I am not comparing X with Y here.
Not talking about package like Guzzle:
I am not mentioning building something like Guzzle, which is a framework that includes the tools needed to create a robust web service client.
But I am interested in talking about the packages which can be used to build something like Guzzle when you look into the require of composer.json.
That means something like Aura.Http which can be used to make a request to google and do the search if needed or post etc.
Each namespace must have a top-level namespace (“Vendor Name”), and a Package name. Probably you will end up with Vendor.Package or Vendor_Package .
2 ) Composer/Eath, are great tools which help you to manage the dependency management in your PHP projects. So we need a composer.json or package.yml .
3 ) We are creating a package or component, so obviously it will contain source files and test files. It will be a mess when we keep all tests and source files in a same place. Most of them uses either src folder or lib or library to keep source files and tests to keep test files. Probably you have some documentation, a docs folder may help. May be some executable scripts. You can also add a scripts or bin folder.
The above structure follows a PSR-0 naming convention for package/component.
NIH : We all create new packages for we don’t like some other package invented here, not for its not invented here or probably to make use of newer stuffs like traits, closures etc available in PHP 5.4 than sticking with PHP 5 or sometimes it is not inveneted here.
So add your depenedencies to composer.json or package.yml
Let us assume we are going to create a Validation pacakge. Probably you love annotations or yml format. It is always a better idea to keep the annotations, yaml etc on a bridge than in the Validation package. Remember you have composer, let the bridge package composer.json helps you to download validation package, annotation library. This also helps you to write your integration tests in bridge package than making a dependency on the Validation package.
And also help people to use different annotations library than a single one or you will end up messing with more annotation classes.
I guess this helps people to
1 ) fork and contribute
2 ) Even if there is no documentation, it helps to look into the source and get the point. Else a lot of classes confuses people.
Nothing is developed to make it bad, everything is made for good. Though at-times it acts badly.
These are some of the principles I learned looking at various projects like Aura, Symfony2, ZF2, Lithium, Fuel2, Illuminate… you name it.
Recently I ported the blog from Drupal7 to octopress for certain reasons. The php script that helped me to do the porting from drupal to octopress is given below. This is a port of the ruby script with some additional fixes on the SQL. This needs pandoc to be installed to convert from Html to Markdown. There is also slight isssue with the syntaxhighlighter, which you may want to manually fix.
<?phpfunctiondrupalProcess($dbname,$username,$password,$host='localhost'){$dsn="mysql:host=$host;dbname=".$dbname;$pdo=newPdo($dsn,$username,$password,$options=array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8',));$sql="SELECT n.nid, n.title, n.created, n.changed, b.body_value AS 'body', b.body_summary, b.body_format, n.status, l.alias AS 'slug', GROUP_CONCAT( d.name SEPARATOR ', ' ) AS 'tags' FROM node AS n JOIN field_data_body b ON b.entity_id = n.nid JOIN taxonomy_index t ON t.nid = n.nid JOIN taxonomy_term_data d ON t.tid = d.tid LEFT JOIN url_alias AS l ON l.source = CONCAT( 'node/', n.nid ) WHERE n.type = 'blog' AND b.revision_id = n.vid GROUP BY n.nid";$sth=$pdo->prepare($sql);$sth->execute();$rows=$sth->fetchAll();$bool=array(0=>'false',1=>'true');foreach($rowsas$row){$fp=fopen('htmlcontents.html','w');fwrite($fp,$row['body']);fclose($fp);$datetime=newDateTime();$datetime->setTimestamp($row['created']);$date=$datetime->format('Y-m-d');$contents='---'."\n";$contents.='layout: post'."\n";$contents.='title: '.$row['title']."\n";$contents.='categories: ['.$row['tags'].']'."\n";$contents.='published: '.$bool[$row['status']]."\n";$contents.='date: '.$datetime->format('Y-m-d H:i')."\n";$contents.='---'."\n";if(!empty($row['slug'])){$list=explode('/',$row['slug']);if(count($list)>1){$row['slug']=end($list);}}else{$row['slug']='node-'.$row['nid'];}$pandoc='';exec('pandoc -t markdown -f html htmlcontents.html',&$pandoc);$contents.=implode(' ',$pandoc);$url='source/posts/'.$date.'-'.$row['slug'].'.markdown';$contents.="\n";if(!file_exists($url)){$fp=fopen($url,"w");fwrite($fp,$contents);fclose($fp);}else{echo"File exists issue : ".$row['slug']."\n";}}}//optional hostnamedrupalProcess('dbname','username','password');
I was working with Imagine recently. I want to create a standard width and height for the image that is created. Eg: 500 X 300 .
The user will be uploading different size images and I want to resize the image to X width and Y height. I was using imagine and the code helps to make this happen filling the blank space with white color without loosing aspect ratio.
A huge Thanks to the person who helped me on Symfony2 irc to find the different modes. I am not recalling the irc handle currently. I will update once I remember the same.