With eZ Publish moving to Symfony2, I found that I was in need of learning some new coding standards. I never really looked into PHP CodeSniffer while doing eZ Publish development (most of my code was in the templates), so I never really thought about PHP CodeSniffer as an option until starting work with Symphony2. To the point, I just wanted to share the Symfony2 PHP CodeSniffer Coding Standard on Github. It is really easy to install and because phpcs works with PHPStorm and Sublime Text 2, sniffing out php files couldn’t be easier.
I have been working on getting a new site theme up and running for a while now. My hold up was that I could never commit to a platform. I knew that I wanted off Wordpress and onto something more developer friendly. Originally I was going to put this site on eZ Publish, but that just seemed like overkill. Next I thought about creating a Symfony2 blog, but I wanted to cheapen my hosting options so I opted against Symfony2. I finally decided upon Rails, hosted on the free tier of Heroku.
I was in the middle of development when I discovered Octopress. How I had never heard of this platform before, I don’t know. In under 4 hours total I had my Wordpress site migrated off of Wordpress and onto a Github Pages site generated by Octopress; it was a very smooth transition. Because development was so fast, I opted to stick with Octopress and GH-Pages.
My site is now running statically using Octopress as a site generator and Github as a free host. Octopress is built on Jekyll, so I can make plugins easily (in Ruby) as well as pull in any existing plugin with ease. Octopress seems like it is built to be the developer blogging platform. The code blocks, the post generation, and the fact that it is built on Ruby all helped me settle on Octopress as my new blogging platform. I hope you enjoy the new site!
Solr has quickly become one of the most popular search engines available. Due to Solr’s many features and its low barrier to entry it can be an ideal candidate when seeking a search engine. Solarium, a PHP Solr Client, takes Solr one step further by giving developers easier access to the Solr API.
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3
Introduction to Solr: Search is an important part of most web projects and should be given much care. Throughout the years, making content searchable has changed a great deal. Obviously, exposing SQL queries to users through search is dangerous. Modern search implementations require a secure approach, such as a stand-alone search server, separate from site data. There are many search options to choose from. Your search list might include: Sphinx, Flax, ElasticSearch, Google, and Solr. Solr’s scalability, speed, built in features and community make it an ideal platform for any project. Luckily, integrating Solr into your PHP project has been made easy by the Solarium Project.
In Apache’s own words, “Solr is an open source enterprise search server based on the Lucene Java search library, with XML/HTTP and JSON, Ruby, and Python APIs, hit highlighting, faceted search, caching, replication, and a web administration interface” . Apache Solr is an open source, extensible, stand-alone search engine, built on Lucene, managed by The Apache Software Foundation. There are currently two stable versions of Solr available for use; Solr 3.6.2 and Solr 4.0. Either version may be used with Solarium; Solr 4 ships with added features, however, so use Solr 4 if possible.
Solr is designed to run as a stand-alone Java web application (app). The documentation for downloading, installing, and running Solr can be found at http://lucene.apache.org/solr/tutorial.html. Because Solr is meant to be separate from the primary project, it can be installed anywhere. All interactions with Solr happen over HTTP through a REST-like API. Solarium utilizes both PHP and JSON for its interactions with Solr.
One of the greatest features of Solr is its scalability. Replication can be set up to run multiple Solr instances in a master/slave setup, just like MySQL replication.The documentation for setting up a master/slave setup is very thorough, so it will not be discussed further here. Another option for scaling Solr is to search multiple search cores. Solr can run multiple search cores at a time. Solr can search multiple cores at a time through its distributed search and sharding capabilities. Large search indexes can be split into multiple smaller indexes; then through a distributed search multiple cores are searched. The best part of a multicore search setup is that not all cores need to exist on the same server. This allows for very flexible search architecture.
Solarium acts as a bridge
Solr has a REST-like API, so interactions with it are very simple and happen over HTTP. The Solarium Project is “an open source Solr client library for PHP applications” , which makes interacting with Solr even simpler. Solarium exists to expose the Solr API through an easy to use PHP library.
Installation of Solarium has been made very easy through the use of Composer (Listing 1). Installing through Composer will automatically add the library to the PHP autoload path. To install Solarium into a project not utilizing Composer, the files will need to be downloaded from GitHub (https://github.com/basdenooijer/solarium/tags). Next, add the solarium library to the PHP autoload path manually. After Solarium is installed, Solr can be accessed through sending a Solarium Client ($client) object to a configuration array. The configuration array should be made up of the Solr host, port and path (Listing 2).
Adding content to Solr
Before anything can be searched in Solr, documents (docs) must be added to the Solr index. Solr maintains a collection of docs in its index. A doc is a collection of fields and values. Solr fields must be defined in the Solr schema. A field can occur multiple times in a doc.
To make an update to Solr using Solarium, start with the
$client object. Using the
$client object, create an
$update instance then make a doc:
With the newly created
$doc, begin adding content to Solr fields as instances of the $doc:
1 2 3 4 5 6 7 8 9
In this example, the Solr schema would have to be set up to accept the id, name and text fields. Solr ships with an example search app with a good demo of a Solr schema; each field will need to be added to the schema in order for Solr to add the docs to the search index (Listing 3). Next, add each
$doc created to an array so that
addDocuments() can be called on the array. This will run the Solr add command for every document in the array:
The newly-added docs will not actually become a permanent part of the Solr index until Solr is told to commit all added docs to its index. To do this with Solarium, call
addCommit() to generate a commit message for Solr, then a call to
update() with the
$update will send the commit:
The new docs should now exist and be searchable using Solr.
Searching Solr through Solarium
Solarium offers a robust PHP API for searching Solr. To run a basic query, start with the $client object. Next, call the
createSelect() instance of the
$client object to generate the query for execution:
1 2 3 4
createSelect() function is being used to generate the query, the
select() function should be used when executing the query:
Use the $resultSet array to iterate over the array and display the results:
1 2 3 4 5 6 7 8 9
$resultSet can also be refined. Setting up pagination, for instance, is very easy:
Sorting is also very easy (note that Solr will sort by score if no sort is set):
Filtering, setting query fields, boosting, and faceting are also possible ways of searching Solr. Filtering is another way of narrowing search results without using a query. Query fields allow for control over what fields are searched. Boosting gives certain field’s higher precedence in the Search. Facets give users a way to better navigate search results. Solarium gives easy access to all the various ways of searching Solr content.
Removing content from Solr
Solr does not have a command called update; however, updates can be performed by re-adding content. Solr uses a unique identifier for each doc and only allows for one instance of the identifier in the search index. To update content, call the add command on an already existent Solr doc id. Sometimes updating Solr with new content isn’t enough. Sometimes the need to delete content from Solr arises. Doing this is similar to when adding docs, create an $update instance:
Using the $update instance, documents can be deleted by id or by query. This offers a great deal of flexibility for deleting content. Deleting by id is used more often because it offers very precise deletes:
1 2 3
Deleting by query, however, can be incredibly powerful and useful. When starting development of a new search project with test content, for instance, deleting by query offers the ability to remove all test content. If all the test content starts with the word test, deleting the content is simple:
1 2 3
Another common practice is to wipe the index and start fresh. Using delete by query, this task is again made very simple:
1 2 3
Solarium’s delete doc API is very useful in maintaining an up-to-date search index.
Solr is an incredibly scalable, powerful and easy-to-use search engine that should be considered for any search project. Solr is designed to be language agnostic through its REST-like API, and thanks to Solarium it fits very easily into any PHP project. This article covers only three use cases for Solarium; the examples shipped with Solarium offer a great deal more. Search highlighting, debugging, optimizing, and ‘more like this’ functionality are all possible with Solr and Solarium.
- Apache Solr: http://projects.apache.org/projects/solr.html
- The Solarium Project: http://www.solarium-project.org/
I am very new to Symfony development. While developing a side project app I found I needed to control access to a certain area to logged in users only. Based on my previous development history I was expecting to have to determine if a user was logged in via the controller. I figured I would need to find an
is_logged_in boolean and use it to determine user access control. What I found while doing my development is that Symfony2 takes care of access control in a much more eloquent way. Rather than having to determine if a user is logged in inside each controller, Symfony2 controls access using a system similar to its routing system. Inside a Symfony2 app there is, by default, a
security.yml file that is used to control access. Inside the
security.yml file locate the
access_control declaration. By simply declaring a route inside the
access_control yml array we can control which users have access to which routes, like so:
1 2 3 4 5 6
As you can see with my code, for anything beyond the
/admin/ route the
ROLE_ADMIN is required. And for anyone accessing the
/new route the
ROLE_USER is required.
This is obviously pretty trivial stuff, but for somebody new to Symfony2 development like myself, I found it an incredibly simple and powerful way of controlling access to my app.
I finally decided on my first eZ Publish 5 project… my site of course. I am currently in the middle of upgrading my wordpress install to eZ Publish. Honestly, I don’t really need anything more than a simple Wordpress install, but it will be nice to get an eZ5 site under my belt.
Seeing as I am in the middle of development, there have been a few bumps in the development road that I have found and would like to share. Firstly, I can find no way to run eZ Publish 5 completely in legacy mode, no matter what I do my pagelayout template needs to be a twig template. I posed the question to the eZ Community and it sounds like this should be possible, but it just wouldn’t work for me (see the discussion here). I actually don’t have a problem with this, it’s not like being forced to use Symfony is a bad thing.
Second, I do not see a strait-forward way to run multiple sites in one eZ Publish 5 install right now. Site settings are declared in the
ezpublish.yml file and are grouped appropriately. My problem is that the
parameters.yml file only allows for the declaration of one default template. In my opinion the default template declaration should be tied to a siteaccess or a siteaccess group.
index_dev.php file doesn’t work for some reason. It would be nice to be able to use the Symfony dev toolbar. So far my only workaround has been to add the dev params to the index.php file. Either fixing this or allowing
DebugOutput on a Symfony generated page would suffice.
It wouldn’t be fair to simply criticize eZ5, because it does work. The eZ Team did a hell of a job with eZ5, far more than I would have expected for a first iteration. Honestly I was expecting eZ5 to switch to the Symfony Components for round 1 and work on the rest later.
I think that far and above any other nicety of eZ5 is the ability to generate custom routes. Creating a custom module in eZ 4 was not exactly easy, but now with Symfony, creating a custom route/controller/view is extremely straight-forward and borderline easy. If a person has ever worked with Symfony in the past, creating custom ‘modules’ will be a snap.
Another awesome part of eZ5 is the full REST API. Although I have yet to use it, I know it is there and that the eZ Team put a lot of work into it, so I have to give it props.
Finally, twig is awesome to work with.
I am finally taking the time to write about optimizing the Solr schema file. In my last post I showed that eZ Find already ships with a default search field called
ezf_df_text. The downside of searching one field is that field needs to be controlled, and the only way it can be controlled is by manually editing the Solr schema file. As you might expect, eZ Find does not control this file very well out of the box.
1 2 3 4
A copyField will take any field you tell it to and copy it into another field. The code above is what ships with eZ Find, every single
*_t (text), and
*_s (string) is added to the
ezf_df_text field. For obvious reasons the average installation probably doesn’t need every piece of content searchable. The first step is to delete the default copyFields (e.g. your site users probably shouldn’t be searched by default).
I have spent a good deal of my freetime this past month or two learning as much about about the php framework Symfony as possible. The moment that eZ Systems announced that they were adopting the Symfony framework I knew that I would be be spending some time with it. I have found that Symfony doesn’t have the lowest barrier to entry so it has been a process, to say the least. I have a blog that has helped matters a great deal and so I wanted to give it some props here. If you are like me, wanting to learn Symfony but lacking time and resources, you should really check this site out: Symblog: Creating a Blog in Symfony2
The blog starts with the basics and explores a good deal of Symfony components in detail. In the end you will be left with a barley usable blog, but that is alright, it leaves you with a project to fix. It is a tutorial for Symfony 2.0, and because Symfony is currently on 2.1.3 you will notice some part don’t work the way you expect (but I think that forces you to learn more anyway). The tutorial also doesn’t use Composer at all; so remember, when the blog tells you to install something, use Composer and skip over the way they tell you how to do it. I hope you enjoy the tutorial, I know I did (I have not currently completed the tutorial but am nearing completion at the time of this writing).
I recently updated to Skype 6 and found something interesting out. My style package that worked in Skype 5 would not work in Skype 6. With some digging, we found out that in order to make your Skype 5 style work in Skype 6 you need to update the
MessageViewVersion to version 7 (I have no idea why). So, with that, have fun updating your Skype to the latest version while still getting use out of your favorite Skype theme.
I try as hard as I can to stay out of politics. I have my opinions like everybody else and I try to not start arguments as often as possible because I also value my friends and family. But with the American presidential election around the corner, I just had to share this bit of gold.
Just had to share this post since I helped a little bit with it. Very handy little tip about authorizing security groups across AWS accounts.
It is a post by @jacksonmurtha so we can trust that it is good.