Running Zend Framework Project from a subfolder

Lets say you want to run your project from a subfolder (in my case ‘xproject’) in your document root (in my case ~/Sites). So you want it to access via http://localhost/xproject and you do not want to be in hassle of creating virtual hosts (which you cannot do in shared hosting at all). These are the steps to do it.

  1. Copy the .htaccess file from you public folder and place it one older above
    (in my case, copy ~/Sites/xproject/public/.htaccess to ~/Sites/xproject)

    $ cp ~/Sites/xproject/public/.htaccess  ~/Sites/xproject/

    Replace the lines there with these:

    RewriteEngine on
    RewriteRule    ^$ public/    [L]
    RewriteRule    (.*) public/$1 [L]
  2. Open the application.ini file of Zend Framework project (in my case ~/Sites/xproject/application/config/application.ini) in text editor of your choice and place this in development section (or your local environment section)
    settings.baseUrl = "/xproject"

    and save it.

  3. Open the Bootstrap file of your application (in my case ~/Sites/xproject/application/Bootstrap.php)

        protected function _initBaseUrl()
        {
            $options = $this->getOptions();
            $baseUrl = isset($options['settings']['baseUrl']) 
                ? $options['settings']['baseUrl']
                : null;  // null tells front controller to use autodiscovery, the default
            $this->bootstrap('frontcontroller');
            $front = $this->getResource('frontcontroller');
            $front->setBaseUrl($baseUrl);
        }

    Save the file.

  4. And you are done. Fire your browser to http://localhost/xproject and you will see the project running

Automated testing with Yii – Resolving Fixtures issue

Hi,

If you are starting using PHPunit testing with Yii, chances are there that you may encounter an error which drove me crazy. This error occurs when you are using the fixtures to test the CRUD operations. Error will be like:

1
2
1) ContactListTest::testCreate
Exception: Unknown method 'contactLists' for class 'ContactListTest'.

You will note that my class name is ‘ContactListTest’ and my fixture file name is ‘ContactList.php’. My table name is ‘contact_list’.

The resolution to this issue is hidden in the documentation of the Yii Testing which states that the fixture file name should be same as that of the table name.

I changed the filename of fixture to ‘contact_list.php’ and it worked!!

 

Bye Bye ugly Content Disposition Attachment with Yii

Hi,

With Yii Framework, you can say good bye to ugly header code for forcing download a file on client side (I usually used for pdf files download forcibly) so that client browser should not try to open in in-built file handler.

1
2
$request = Yii::app()->getRequest();
$request->sendFile('abc.pdf', file_get_contents(Yii::app()->basePath.'/files/mypdf'))

As Per Yii Docs, this function takes care of all the headers required to accomplish the task. So all clean work now.

Sending Emails from RoR Application hosted on Site5 Shared server

Use this code:

1
2
3
4
5
6
7
8
9
10
11
12
default :from => "noreply@domain.com"
config.action_mailer.delivery_method = :smtp
   config.action_mailer.smtp_settings = {
   :address => "smtp.domain.com",
   :port => 25,
   :domain => "domain.com",
   :user_name => "noreply@domain.com",
   :password => "password here",
   :authentication => :login,
   :enable_starttls_auto => false,
   :openssl_verify_mode  => 'none'
}

Executing PHP script from shell using exec() in a PHP script

If you are trying to execute a php script from shell using exec() or passthru() functions within a php script then you may end up with a hung apache thread or non-execution of the to be executed script.

I was doing the same in one of my projects where I need to import a big list of items (half million) in a project built on Yii Framework using a CSV upload. Coding was written, but the import failed due to large number of validations to be run before importing. So, I thought to create a Console App in Yii and then call this Console App from my Yii Controller action like this:

1
exec('php '.Yii::app()->basePath.'/yiic.php '. $consoleApp .' > /dev/null 2>&1 &');

This worked well on my MacBook Pro but when the same code was executed on the live server (CentOs with PHP 5.2.17), the script did not execute at all. I spent full 4 days and tried all sorts of techniques to get it executed but in vain.

Then, I replaced php with php-cli in the command

1
exec('php-cli '.Yii::app()->basePath.'/yiic.php '. $consoleApp .' > /dev/null 2>&1 &');

And it worked well! Its a bug in PHP that you cannot use php interpreter to execute another php script when it is already executing your script from http or browser. So, the workaround is use ‘php-cli

Happy Yii-ing !

Relative URLs for images in TinyMCE – Not Good for sending Newsletters

If you are planning to use TinyMCE for the HTML content of a Newsletter Module, keep in mind that any images you put in the editor will bear the relative URL by default. The relative URLs work ok when the things are to be viewed on site. However, a newsletter has to travel across the world to different inboxes. If the default settings of TinyMCE are used, the end client will see a Big ‘X’ instead of image. Because the image path is not absolute, image cannot be shown.

Here is the solutions:

You have to add three parameters in the TinyMCE’s init section which will force it to use absolute paths. These are:

1
2
3
document_base_url : "http://domainname.com",    
relative_urls : false, 
remove_script_host : false,

So, your code looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
tinyMCE.init({
        // General options
        mode : "textareas",
        theme : "advanced",
       // width: "900",
        height: "400",	
        editor_selector :"mceEditor",
        plugins : "paste, table, inlinepopups, fullscreen",
        theme_advanced_buttons1 : "bold,italic,underline,|,cut,copy,paste,|,forecolor, backcolor|,bullist,numlist,|,pastetext,pasteword,link,unlink,fontselect,fontsizeselect,|,image,code,|,table,fullscreen",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
       	theme_advanced_resizing : true,
        skin : "o2k7",
	document_base_url : "http://domainname.com",    
	relative_urls : false, 
        remove_script_host : false,
        skin_variant : "silver"
 
});

Just save it, refresh the page with TinyMCE and see the source, you will see that the paths have been changed. Now save the content of TinyMCE by submitting the form and you are done.

Find and Replace text in multiple files in Linux

Its quite easy for finding and replacing some text contained in the editable files in Linux with ‘sed’ command. Below is a case to replace some text in all the php files on the server containing text ‘old_text’ to ‘new_text’:

1
find public_html/ -type f -name "*.php" | xargs sed -i 's/old_text/new_text/g'

The First part before pipe (|) sign find s the file with php extension and the second part of file finds and replaces the text in the found file.

Easy enough :)

Keep Hacking!

Cannot add foreign keys in table – Mysql

While creating the mysql database, I received an error that that fallback script failed and obviously the foreign key was not added. So here’s the solution:

Mysql expects every foreign key in database to be unique so no two foreign keys should have the same name.

I renamed the newly created foreign key and everything worked like a charm.

iTextSharp issue, That assembly does not allow partially trusted callers

iTextSharp issue, That assembly does not allow partially trusted callers

2. Then you have to modify the AssemblyInfo.cs file by adding these references and attribute:
1
2
3
using System.Security;
using System.Security.Permissions;
[assembly: AllowPartiallyTrustedCallers]
3. You should recompile itextsharp and copy again in your bin directory (depending on the way you use it).