Yii : route config for subdomains

April 29th, 2015 § 0 comments § permalink

Recently I was setting up a subdomain for a existing project, I needed to configure a different root route for the it. First I tried to do it with htaccess but I failed. Then I found a solution after some googling.

// protected/config/main.php
array(
    'urlManager' => array(
      'urlFormat' => 'path',
      'showScriptName' => false,
      'caseSensitive' => true,
      'rules' => array(
        'home' => 'site/index',
        'http://about.site.dev/' => 'about/index',
      ),
    )
  )
);
?>

This will result in two different root route for site.dev and about.site.dev. I you know a better solution please let me know.

PHP : Add element in the beginning of an array

August 6th, 2013 § 0 comments § permalink

We can add an element in the beginning of an array using array_unshift method. Its quite easy and handy, But did you know that array_unshift will reindex you array? Thats what I like to discuss over this blog post.

Lets take the above php array as sample. I intentionally missed the indexes 3 and 4 to show array_unshift method will reindex your array.

See the Fifth is moved to 3rd index? This will happen only when the indexes are numeric.

So the easy way to append an element in the beginning of an array without reindexing is using “+” operator on array.

Now the “Fifth” element is still on index 5.
Hope you will be careful when using array_unshift next time.
Thanks.

Install phpunit on ubuntu

February 21st, 2012 § 0 comments § permalink

A few months before I wrote an article on how to install phpunit on windows. Now when I tried to install phpunit on ubuntu I faced an error phpunit/PHPUnit requires PEAR Installer (version >= 1.9.4), installed version is 1.9.2 .

Here is the steps which I used to get rid of this error.

  1. Install php-pear
     sudo apt-get install php-pear
  2. Update its own channel
     sudo pear channel-update pear.php.net
  3. Upgrade pear
    sudo pear upgrade-all
  4.  Now install PHPunit
    sudo pear install –alldeps pear.phpunit.de/PHPUnit 
Now we can check whether installation is success by typing phpunit in terminal.

Yes. It worked.

Happy Testing.

PHP : How to avoid isset on array index

February 3rd, 2012 § 0 comments § permalink

I think most of you are fed up with checking each and every array index with isset function. Here is the simple way to avoid isset on array index. When I was a PHP newbie, I used to write code like this to avoid Notice : undefined index “blahblah”

In this case, we are helpless to predict which all index will be missing but we can use a better way which we can make sure that no array indexes will be missing.

Now we can assure that in $data all the index are present. So we can avoid isset checking.

Happy coding.

Get last date of this month in php

February 6th, 2011 § 3 comments § permalink

This tutorial helps you to find last date of the month. It’s really easy to find it with “date” and “strtotime” functions.

It is a simple as

Last date of this month

echo date("Y-m-d",strtotime("+1 month -1 second",strtotime(date("Y-m-1"))));

Last date of next month

echo date("Y-m-d",strtotime("+2 month -1 second",strtotime(date("Y-m-1"))));

Last date of previous month

echo date("Y-m-d",strtotime("-1 second",strtotime(date("Y-m-1"))));

The logic behind this is quite simple. Any way the first date of every month will be “1” .We only have a confusion on the last date ie., whether it will be 28,29,30 or 31.

So first we will get the first date of this month by `date(“Y-m-1”)`. For eg. which result us `2011-02-01`.With the help of `strtotime` function i can convert the date into Unix time stamp and with the help of the same function i can add 1 month to it. `strtotime(“+1 month”,strtotime(date(“Y-m-1”))));`. So now it return the Unix time stamp corresponding to  `2011-03-01`. Subtracting a second from this will result you the last date of this month.

Happy coding.