Category: PHP

Adding Layout support like RubyOnRails into CodeIgniter :: By Hooking

Recently I came across an interesting feature in RoR which is known as layout yielding. To understand what it is just imagine that you have a particular template file (layout) specific to your controller, and in that layout file you have a special block called yield. After that whenever there is some output from any method in your controller will be yielded exactly to that block in that layout. Which means all output from your controller will be first replaced into that “yield” block inside that layout, and then whole data will be displayed right away.

So what is the benefit of using that. Simple, it will help to design your website more flexibly. You have a layout, and inside that layout at a particular position you can output all data. This will just minimize the need of splitting a single template into smaller pieces like “header”, “body”, “footer” and so forth, and finally turning them into a single piece manually before rendering the final output.

Let’s have a look at the following image to understand the fact more clearly.

layout.gif

This is a typical website payout where some of its sections are marked. In this layout which sections you think changes most frequently? Definitely the section marked as p3. Now what usually happens when you design a site.

1. Either you write the whole file as template and change the parts you need
2. Or you split them into smaller components and merge those parts while delivering the final output.

Now, with this yield feature all we do is we create a single html file exactly as the design keeping the div of P3 blank. But inside that P3 container div, we will just write some text “{yield}”. Now because of this yield feature, whenever our controller generate some output using any view, CodeIgniter will automatically load that layout file first, then take the output from the view and merge them together.

To enable this excellent yield feature into CodeIgniter we need to write some hook. First, open the file named application/config/hooks.php and add the following line in it.

$hook['display_override'][] = array(
'class' => 'Yielder',
'function' => 'yield',
'filename' => 'Yielder.php',
'filepath' => 'hooks'
);

After that, inside application/hooks folder create a file named “Yielder.php” and inside that file, write the following code.

<? if (!defined('BASEPATH')) exit('No direct script access allowed');
class Yielder
{
function yield()
{
$ci= & get_instance();
$current_output = $ci->output->get_output();
$controller = $ci->uri->segment(1);
$layout = "system/application/layouts/{$controller}.php";
if (file_exists($layout)){
$output = $ci->load->file($layout,true);
$output = str_replace("{yield}",$current_output,$output);
}
else
$output = $current_output;
echo $output;
}
}
?>

And that’s it!! You are done.

Now all you have to do is create a folder inside your application folder named “layouts” and place the layout file named same as controller. For example if your controller name is “abcd” then your layout should be “abcd.php” – But remember to keep a section named {yield} inside that layout. Lets take a look at the complete example below

Controller: controllers/abcd.php

<?
class abcd extends Controller
{
function help()
{
$title = "";
for($i=0; $i";
}
$this->load->view("myviews",array("title"=>$title));
}

}
?>

View : views/myview.php

<?
echo $title;
?>

Layout : layouts/abcd.php

<h1>
Hi there
</h1>

{yield}

<h1>
Happy Ending
</h1>

And the final Output is

/****************************

Hi there

0
1
2
3
4
5
6
7
8
9

Happy Ending

****************************/

Just PHP will give you nothing… unless you upgrade yourself

I have been working with PHP for more than 3yrs (I believe still I am beginner in this category) – I was present in several interview board. Which things disappointed me most is the “lack of eagerness” to learn what comes new. Sometime developers thinks that learning Only PHP will help them to get lucrative jobs!! OMG

Specially in BD most of the time PHP developers plays multiple roles in the companies, they are developer, they are template designers, they are HTML coders, they are DBA, they are PMs….what the heck. Only few companies have different people for these roles.

How far you can go just learning PHP (RAW code, in ancient style, that means PHP+HTML together, yak!!)? You have to have knowledge in CSS, JS, Frameworks, Multiple DBS

When it comes the question of CSS, you should maintain a list of websites where from you can get updates. Dont learn the CSS from book, but goto websites and see what is happening… If you are still using Tables to design (more…)

@Full Throttle

Finally I got my PHP team in somewherein. Morshed and Lavlu joins the team this 15th. Rashed an Ryan are doing exciting artworks for overall look-n-feel. Fariha and Jana are maintaining the policies. We are running at full throttle.

PF members are leaving for KL tomorrow. Hope I can join them pretty soon.

We are running at full throttle for this Blog. Yahoooooo

Just signed for my third book



Today I signed contract for my third book. Well, I cant disclose the title or anything here yet, its just a news. I hope that I can inform you details about my upcoming title within a couple of months.

Thanks for reading my blog!!!

Count the occurrence of words in a string


If you want to count the occurrence of words in a string, the following routine may help you. For example if you supply the string “hello hello Hello I am am Hasin” it will out put the followng

hello = 3
I = 1
am = 2
hasin = 1

Here is the code

<?php
$str = “hello hello Hello I am am hasin”;
$word_count = (array_count_values(str_word_count(strtolower($str),1)));
ksort($word_count);
foreach ($word_count as $key=>$val)
echo $key .” = “. $val.”<br/>”;
?>

I got a cool idea – Now developing this wordpress plugin

After getting the solution for sending message to MSN contact I got another cool idea. I can easily develop a small plugin for wordpress using which people will be able to subscribe to any blog with their MSN address. and When that blog will update, that plugin will automatically send new post URL to those subscribed MSN addresses.

Whoa!!

I have started developeing this cool plugin for wordpress.

Replace bad words in a string

Recently I came accross a pretty interesting problem through my group, phpexperts where someone needed a solution to replace badwords, but not if that word is a part of another word. For example if you consider the word “some” as bad, then you can replace all “some”, “.some”,”some,” but not “someone”.

Here is a solution I created for that prob
<?
$var = "somesomesome somesome some, some. someone .some ";
$badword = "some";
$var = preg_replace(array("/([\W]+){$badword}([\W]+)/","/([\W]+){$badword}([\W]+)/"),"$1".str_repeat("*",strlen($badword))."$2",$var);
echo $var;
?>

Cool Messenger Libraries for Developers

I have been planning to develop a localized messenger service for Bangladeshi peoples for quite a long time. I studied a lot on this topic and many times attempted to kick start the development, but unfortunately I ran out of time every time. But what I found is a wide range of libraries available for developers to develop such a service without spending a great amount of time. Let me list some of them here for You

1. Wildfire IM Server with IM Gateway [Java]
2. sendMsg [PHP For MSN (The Best)]
3. MSNP [PHP for MSN]
4. Joscar [Java for AOL/ICQ]
5. jYMSG[Java For YM]
6. TjMSNLib [Java for MSN]
7. Java-Jml [Java for MSN]

Are you interested????

Acessing secured content via PHP and CURL

If you are looking for a solution to access content over SSL, please take a look at this solution. Here i am fetching content from del.icio.us over SSL. the trick is you have to accept SSL certificates by setting CURLOPT_SSL_VERIFYPEER to false.

<?php
// HTTP authentication
$url = “https://api.del.icio.us/v1/posts/all”;
$ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, “username:pass”);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($ch);
curl_close($ch);
echo htmlspecialchars($result);
?>