Force Zend Framework to use the index controller by default

Maze

Everyone wants pretty URLs these days—both for convenience and to optimize for search engines. So having URLs with unnecessary information is a major no-no. Over the past year I’ve been slowly absorbing the Zend Framework and its MVC pattern. Historically, projects I created required the user to specify the index controller like so:


http://www.mysite.com/index/page

…where “index” is the controller and “page” is the action. Since almost all people will be using the index controller, why is it in the URL? Want to get rid of it? Add the following to your application.ini file in the production section:


# Routes
resources.router.routes.default.route = /:action
resources.router.routes.default.defaults.controller = index
resources.router.routes.default.defaults.action = index

Now your application will use the index controller by default so your URLs will be even prettier:


http://www.mysite.com/page

But wait! What if you need to access a different controller? Maybe an admin controller (or module)? Add this beneath the previous addition:


resources.router.routes.admin.route = /admin/:action
resources.router.routes.admin.defaults.controller = admin
resources.router.routes.admin.defaults.action = index

The downside, of course, is that for each additional controller you create, you’ll need to add these three lines to your application.ini file. Luckily for me, I don’t anticipate having very many controllers.

While this technique may seem obvious, I couldn’t find it anywhere on Google. So, if you’re successfully doing this some other way, please share in the comments.

More on the Zend Framework Router

Share

Pillars of Faith

Last week in our Disciple II class with Jay (scroll down), he asked us to come up with five verses or passages from the Bible that give us hope and sum up why we are Christians.

1. Matthew 17:20

He replied, “Because you have so little faith. Truly I tell you, if you have faith as small as a mustard seed, you can say to this mountain, ‘Move from here to there,’ and it will move. Nothing will be impossible for you.”

I find comfort in that because I realize my faith is ever-growing and yet I can still do things through God already. How much more will I be able to do as my faith continues to mature.

2. Deuteronomy 31:6

“Be strong and courageous. Do not be afraid or terrified because of them, for the Lord your God goes with you; he will never leave you nor forsake you.”

God is always there for us.

3. Psalm 23:6

Surely your goodness and love will follow me all the days of my life, and I will dwell in the house of the Lord forever.

I can’t think of anything better than knowing I will always be in God’s presence.

4. Psalm 100

1 Shout for joy to the Lord, all the earth.

2 Worship the Lord with gladness;
come before him with joyful songs.

3 Know that the Lord is God.
It is he who made us, and we are his;
we are his people, the sheep of his pasture.

4 Enter his gates with thanksgiving
and his courts with praise;
give thanks to him and praise his name.

5 For the Lord is good and his love endures forever;
his faithfulness continues through all generations.

This one speaks for itself.

5. Genesis 1:1

In the beginning God created the heavens and the earth.

We can never forget that God was and is and always will be. It is our duty to glorify Him with our lives.

What are some of your favorite and inspiring Bible verses or passages? Please share them in the comments!

Share

Caching With Zend Framework Using Zend_Cache

Cheeks Blowing

Today I taught myself how to use Zend_Cache and implemented it within 20 minutes. It’s super easy and very effective. Take a look at the code sample below and you’ll be up and running in no time.

 

Step 1: Setup the Cache
{code type=php}
$frontendOptions = array(
‘lifetime’ => 180, // Cache for 3 minutes
‘automatic_serialization’ => true
);

$backendOptions = array(‘cache_dir’ => dirname(__FILE__) . ‘/cache/’);

$cache = Zend_Cache::factory(
‘Core’,
‘File’,
$frontendOptions,
$backendOptions);
{/code}

Step 2: Use the Cache
{code type=php}
$data = null;
if(!$data = $cache->load(‘data’))
{
$service = new Service(API_KEY);
$result = $service->generateReport();
$data = $service->getReport();
$cache->save($data, ‘data’);
}
else
{
print(“Cache Hit!”);
}
{/code}

The page load time went from about 9 seconds to 0.5 seconds! 18x faster and it only took a few lines of code. Awesome.

My main motivation for caching the data ($data in the code example) was actually to reduce the load on the web service which provides the data. We have a good relationship with the company providing the service but there’s a good chance they would become annoyed if we hammered their system to get the exact same data over and over. The load time improvement was a good side effect, though!

For more information on Zend_Cache which comes with the Zend Framework, check out the reference guide and API documentation.

Share

How to backup your website

Hard Drive in Flames

Everyone knows (or should by now) that cheap web hosts (Bluehost, Dreamhost, MediaTemple, etc.) don’t backup your data for you. So you’d better do it yourself. If you’re on any respectable host, you should have ssh access to the box.

Connect to your box via ssh and run the following commands to create a backup of your site.


cd ~
mkdir Backup
nohup zip -r Backup/YYYY-MM-DD-HHMM.zip www/ > backup_log.txt &

(Replace YYYY with the 4-digit year, MM with the 2-digit month, HH with the 24-hour format of the hour, and MM with the 2-digit minute)

cd ~ navigates to your home folder

mkdir Backup creates the backup directory in which the backups will be stored

nohup is short for no hangup and allows processes started by users at the terminal to continue running even after the user logs out

zip is a program which combines many files into one and compresses them to make the end result even more portable

-r tells zip to burrow into all subdirectories in order to grab all of the files

Backup/YYYY-MM-DD-HHMM.zip is the path to the backup file

www/ is the directory to backup (it may be html, htdocs, httpdocs, etc. on your box)

> backup_log.txt redirects all output from zip to the backup_log.txt file so you can review the file later

& tells linux to run the zip program in the background so that you can logout or perform other tasks without killing the process

Now all you need to do is download that zipped file. Use your favorite SFTP client to login to your box and snag it. I recommend FileZilla Client for all platforms. If you’re looking for an FTP server, FileZilla Server is perfect.

Share