My Science Is Better

19Jan/100

Setup ZF using svn externals

After you create a project with zf tool, put it on SVN, you might want to include inside /library the ZF, but you want to have the bleeding edge all the time. You can include it as a svn external like this:

cd library/
svn propset svn:externals 'Zend http://framework.zend.com/svn/framework/standard/trunk/library/Zend/' .

Next time when you run svn up, your project will fetch the latest ZF from it's repo.

19Dec/090

Setup controllers and actions inside a module using zf tool

I've been trying to move some old code into separate modules for easier use later on. I installed zend framework and zend tool and created a new project:

zf create project myproject

Adding a new module is easy as pie:

zf create module mymodule

This creates the /application/modules/mymodule folder with complete MVC structure inside (controllers, models and views folders).

My module was not working so after a bit of looking around i've found Akrabat's post that some light on the subject. You have to add to /application/configs.ini the following inside the [production] section:

# enabling modules
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""

Good, now we should have a working module, except we don't have any controllers. Trying to create a controller proved a bit tricky as zf tool would not pleace them in the right place, inside the modules/mymodule/controllers but in the default /application/controllers. The same applies for actions. With a bit of help from TheAshMan on #zftalk i found the right answer:

# create controller Photos_IndexController at /application/modules/controllers/Photos_IndexController.php
zf create controller Photos_Index 1 mymodule
# create the action view inside the Photos_IndexController
zf create action view Photos_Index 1 mymodule

Hope this helps someone as it took me a few hours to figure it out :)

22Sep/093

How to update your Twitter status using Zend Framework

After reading the NetTuts tutorial on how to update your Twitter status using CodeIgniter, I wanted to show how to update your Twitter status using Zend Framework.

For this tutorial you need to install Zend Framework and Zend_Tool first.

Step 1: setup apache vhost by creating the /etc/apache2/sites-available/twitter as follows:

<VirtualHost *:80>

    ServerAdmin romeo.cioaba@spotonearth.com
    DocumentRoot /home/mimir/Zend/workspaces/DefaultWorkspace7/twitter
    ServerName twitter.dev
    ServerAlias www.twitter.dev
    ErrorLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_error_log
    CustomLog /home/mimir/Zend/workspaces/DefaultWorkspace7/logs/twitter_access_log combined

    <Directory "/home/mimir/Zend/workspaces/DefaultWorkspace7/twitter/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
    </Directory>

</VirtualHost>

You will need to change the DocumentRoot and Directory directives to match your configuration.

Step 2: add twitter.dev to your hosts file:

127.0.0.1	twitter.dev www.twitter.dev

Step 3: create the Zend Framework project

# i switch to my Zend Studio workspace, where apache is reading his sites from:
# cd Zend/workspaces/DefaultWorkspace7/
zf create project twitter

Step 4: restart apache

sudo /etc/init.d/apache2 restart

At this point you should have a default Zend Framework project that you can browse at http://twitter.dev/public/. How let's change our status :)

Replace the content of IndexController with the following:

<?php
class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // we create the form
        $updateForm = new Zend_Form();

        $status = new Zend_Form_Element_Text('status');
        $status->setLabel('New Twitter Status')
               ->setRequired(true)
               ->addFilter('StripTags')
               ->addFilter('StringTrim')
               ->addValidator('NotEmpty');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Update');

        $updateForm->addElements(array($status, $submit));

        // we send the form to the view
        $this->view->updateForm = $updateForm;

        // we check if there was any POST
        if ($this->getRequest()->isPost()){
            $formData = $this->_request->getPost();
            // checking if the form data is valid (if we have a new status or not)
            if ($this->view->updateForm->isValid($formData)){
                // our form is valid, we can update our status
                $twitterStatus = $formData['status'];

                $twitter = new Zend_Service_Twitter('myusername', 'mypassword');
                $response = $twitter->status->update($twitterStatus);
            }
        }

    }

}

Also replace the view for the index action of IndexController with:


<h1>Welcome to the Twitter Update Tutorial</h1>

<?php echo $this->updateForm?>

TADA! You can now check your twitter account and see that twitter status is updated every time you submit the form -:)

22Sep/090

Install Zend Framework and Zend_Tool using PEAR on Ubuntu

I work every day with Zend Framework and the easiest way i find to install it is by using the PEAR packages offered by the betta channel ZF Campus

Here is what you have to do install Zend Framework and Zend_Tool using PEAR on Ubuntu:

sudo pear channel-discover pear.zfcampus.org
sudo pear install zfcampus/zf
# for some reason the installer does not correctly link the zf.php and Zend_Tool won't work
# so we need to setup a symlink
sudo ln -s /usr/share/php/zf.php /usr/bin/zf.php

That's it! You can now test your install:

zf show version

In my care the output was this one:

mimir@orion:~$ zf show version
Zend Framework Version: 1.9.2
24Mar/092

Zend Framework tutorials – RFC

After a talk on #zftalk I'm thinking to start witting a site that would allow the Zend Framework community to submit learning material.

The site is going to be called Zend Framework Tips

I have a few ideas of my own, but i want to hear more from you.

   

Tag Cloud

Categories

Archives

Akismet

Blogroll

Ads