<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Revolves &#187; PHP</title>
	<atom:link href="http://www.revolves.net/category/programming/php-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.revolves.net</link>
	<description>Innovation</description>
	<lastBuildDate>Wed, 05 May 2010 14:30:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Zend Acl With CodeIgniter</title>
		<link>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/</link>
		<comments>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 13:27:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.revolves.net/?p=24</guid>
		<description><![CDATA[You&#8217;ll need to scroll horizontally to see the source codes. Best is to copy them and paste in your text editor I was just taking a look at the CodeIgniter forum, and found that many people require some sort of an Acl component to make their lives easier. Of course, Zend framework has an excellent [...]]]></description>
			<content:encoded><![CDATA[<p><em>You&#8217;ll need to scroll horizontally to see the source codes. Best is to copy them and paste in your text editor</em><br />
I was just taking a look at the CodeIgniter forum, and found that many people require some sort of an Acl component to make their lives easier. Of course, Zend framework has an excellent tried and tested Acl component. Also, the framework&#8217;s loose coupoling allows to just use a small part of it.</p>
<p>Seeing this, I tried implementing Zend&#8217;s Acl component into a default CI installation. Now, this is a very dirty work, I&#8217;ve just thrown stuffs here and there. But you&#8217;ll at least get an idea on how to use it. The major code goes into a CI library, which initializes the Acl object, roles, resources and rules for them.<br />
<span id="more-24"></span><br />
Lets begin with the directory structure,</p>
<p>I created a new directory called <i>Custom</i> inside the <i>application</i> directory. And here&#8217;s how the folder structure looks inside of <i>Custom</i>,</p>
<p>Custom<br />
 -Zend<br />
  &#8211;Acl<br />
  &#8211;Acl.php<br />
  &#8211;Exception.php (You&#8217;ll need this file too)</p>
<p>If you download the Zend framework, those are the only files you&#8217;ll need. Remember, you&#8217;ll need to have a folder called <i>Zend</i>, since this is how Zend refers to them in all of its includes. I apppend the <i>Custom</i> directory into PHP&#8217;s include path, since putting the <i>Zend</i> directory directly into the include path throws errors. That&#8217;s the reason I have the <i>Custom</i> folder in the first place. You&#8217;ll see all the include path stuff ahead, so don&#8217;t start worrying about them now.</p>
<p>Lets go to the library part. Now, lemme tell you beforehand that I&#8217;ve made no effort to organize code. My objective was to make the Acl component work in a convenient way. So pardon the bad coding practice if any <img src='http://www.revolves.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p>In the <i>libraries</i> folder within your <i>application</i> folder, created a new library file named Zacl.php (I know I&#8217;m not creative). This is it&#8217;s contents.</p>
<pre class="brush: php;">&lt;?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Zacl
{
    function __construct()
    {
        session_start();
        //Append Zend's folder in PHP's include path
        set_include_path(get_include_path() . PATH_SEPARATOR . BASEPATH . &quot;application/Custom&quot;);

        //Load the Acl class
        require_once 'Zend/Acl.php';
        require_once 'Zend/Acl/Role.php';
        require_once 'Zend/Acl/Resource.php';

        //Create a new Acl object
        $this-&gt;acl = new Zend_Acl();

        /**
         * Add roles and resources. Check Zend's documentation for excellent
         * information on all these.
         * http://framework.zend.com/manual/en/zend.acl.html
         */
        $this-&gt;acl-&gt;addRole(new Zend_Acl_Role('guest'));
        $this-&gt;acl-&gt;addRole(new Zend_Acl_Role('users'),array('guest'));

        /**
         * Add some resources
         */
        $this-&gt;acl-&gt;add(new Zend_Acl_Resource('users_login'));
        $this-&gt;acl-&gt;add(new Zend_Acl_Resource('users_profile'));

        /**
         * Set rules for Acl
         */
        $this-&gt;acl-&gt;deny(); //Deny everything, so as to follow a whitelist approach.
        $this-&gt;acl-&gt;allow('guest','users_login');
        $this-&gt;acl-&gt;allow('users','users_profile');
    }

    function check_acl($resource)
    {
        if (!$this-&gt;acl-&gt;has($resource))
        {
            return 1;
        }

        if (isset($_SESSION['user_id']))
        {
            $role = 'users';
        }
        else
        {
            $role = 'guest';
        }

        return $this-&gt;acl-&gt;isAllowed($role,$resource);
    }

}</pre>
<p>Now, I have a constructor, which sets up everything. First of all, don&#8217;t be scared by the <i>session_start()</i> stuff, I just included it for a simple login/logout mechanism you&#8217;ll see later, to check if everythings fine. Even though I&#8217;ve commented the source code, I&#8217;ll just give a gist of what I&#8217;m doing.</p>
<p>1. Loading all required classes for Zend Acl.<br />
2. Adding Roles.<br />
3. Adding Resources (I elaborate on this below)<br />
4. Set access rules</p>
<p>If you see, the &#8216;users&#8217; role is actually a child of &#8216;guest&#8217; role, thus inheriting properties from it. You&#8217;ll need to check Zend&#8217;s manual to know about the syntax and stuff, since its explained quite well.</p>
<p>Now, both role and resource names are arbitrary. You can set them as whatever you want. I&#8217;ve used a small convention for resource part. I&#8217;ve named resources as &#8220;controller_action&#8221;. This is because, I can easily get controller and action name from the URI, and can easily query Acl from any controller.</p>
<p>In the next part, I simply set the access rules (quite self explanatory). I have a function called check_acl(), which takes a parameter named $resource. This is passed by the controller to the function, using the convention I told you about above. Zend throws an exception if a given resource couldn&#8217;t be found. Thus, if a resource is not in the Acl, I return 1, saying that its allowed to be accessed.</p>
<p>If the user has a session variable user_id, it means he&#8217;s logged in, thus I set his role to &#8216;users&#8217;. Else, his role is &#8216;guest&#8217;.</p>
<p>Now, to the controller path. I created a controller called Users, having this,</p>
<pre class="brush: php;">&lt;?php

class Users extends Controller
{
    function __construct()
    {
        parent::__construct();
        $this-&gt;load-&gt;library('zacl');

        $resource = $this-&gt;uri-&gt;segment(1) . '_' . $this-&gt;uri-&gt;segment(2);
        if (!$this-&gt;zacl-&gt;check_acl($resource))
        {
            $this-&gt;load-&gt;helper('url');
            redirect('/users/login');
        }
    }

    function login()
    {
        echo &quot;The login page&quot;;
    }

    function profile()
    {
        echo &quot;The restricted profile page&lt;br /&gt;&quot;;
        echo &quot;Your user id: &quot; . $_SESSION['user_id'];
    }

    function setsess()
    {
        $_SESSION['user_id'] = 1;
    }

    function remsess()
    {
        unset($_SESSION['user_id']);
    }
}</pre>
<p>In the constructor, I load the Acl library, and pass the controller_action to check_acl() function. If the output is 1, user is allowed, else he isn&#8217;t. If he isn&#8217;t, I redirect him to login page.</p>
<p>The login() and profile() functions are simply dummy functions, to check if Acl is working properly. If you look at the library, you&#8217;ll find that guest cannot access profile, while only a user can. The setsess() and remsess() functions are to set and remove user session to simulate logging in and logging out.</p>
<p>Well, I guess that&#8217;s it. I know I went a bit fast, but I really got tired typing this much. Anyways, if you have any sort of doubts, or if my code has some errors, please point out by commenting below! I&#8217;ve tested the above code in WAMP.<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/" rel="bookmark" title="December 3, 2008">Playing With Zend Framework 1.7</a></li>
<li><a href="http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/" rel="bookmark" title="December 7, 2008">An Update On My Zend Framework 1.7 Experience</a></li>
</ul>
<p><!-- Similar Posts took 3.962 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>An Update On My Zend Framework 1.7 Experience</title>
		<link>http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/</link>
		<comments>http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 16:22:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.revolves.net/?p=20</guid>
		<description><![CDATA[I&#8217;ve fiddled with Zend Framework a bit, and even developed a simple MVC application structure, i.e. just the bare bones. Looking at my code, I though, initializing so many classes, is this Framework efficient? Efficiency is the concern of many programmers if their project is going to be hammered with high internet traffic. I noticed [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve fiddled with Zend Framework a bit, and even developed a simple MVC application structure, i.e. just the bare bones. Looking at my code, I though, initializing so many classes, is this Framework efficient?<br />
<span id="more-20"></span><br />
Efficiency is the concern of many programmers if their project is going to be hammered with high internet traffic. I noticed certain slowdowns in various requests, especially querying the ACL and Auth. But that was fine, since I didn&#8217;t do the necessary caching of configuration files.</p>
<p>But still, the amount of overhead was quite large if you&#8217;re planning on using a standard shared hosting. Luckily, Zend does provide a performance guide on the Zend Framework manual. If you follow it, you&#8217;ll surely be able to significantly improve the framework&#8217;s performance, but I still doubt if shared hosting would handle it with ease.</p>
<p>I&#8217;m currently developing a project with <a href="http://codeigniter.com/" rel="nofollow">CodeIgniter</a> 1.7.0 (the current version as of now), and it seems to be more than enough for what I need. With CI, the advantage is that you can start straight away, seeing the screencasts available on their websites. You&#8217;d understand most of the structural parts there itself, and a well written User Guide always to your rescue.</p>
<p>Its not that one framework is better than the other. Zend is definitely powerful, and I&#8217;m still learning new things about it everyday. I&#8217;m sure I&#8217;ll get an opportunity to use it in a real life project.</p>
<p>The choice of a framework should always be based on your convenience, your requirements, the overhead, and the platform selected for deployment. If I&#8217;m simply making a Contact Us form, I wont even be needing a framework (duh!).</p>
<p>I created a simple &#8220;test&#8221; application in CakePHP. Unfortunately, in my shared hosting, its caching didn&#8217;t work. The problem persisted even with default CakePHP setting (all directory structure etc.). The problem seems to be with the shared host, which I no longer use. I don&#8217;t really know what the problem was, but now everything&#8217;s fine with a good new host. CakePHP still is in RC3 as of now, and RC2 at the time of writing my &#8220;test&#8221; application.</p>
<p>Writing it took about a two to three days, because I was still learning CakePHP&#8217;s ORM and Naming Conventions, which I had to constantly refer to. It&#8217;ll become a habit soon, just need more experience. Because of the caching problem, I rewrote the entire application in a day (hey, it was really small) in pure PHP and Smarty especially for its built-in cache support and design/code separation.</p>
<p>It wasn&#8217;t that difficulty after all. But if I were to really develop the application, I&#8217;d have used a framework, for future scalability. For me, CI looks like a good choice for nice code organization, as well as basic Active Record and Validation support. Its quite feature rich, I must admit.</p>
<p>CakePHP is a real monster, but I&#8217;m still waiting it to come out of RC. As far as Zend is concerned, its quite great too, but I&#8217;ll need a resourceful server (not a weak shared hosting) for any serious decent traffic website.</p>
<p>Happy coding!<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/" rel="bookmark" title="December 3, 2008">Playing With Zend Framework 1.7</a></li>
<li><a href="http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/" rel="bookmark" title="December 18, 2008">Using Zend Acl With CodeIgniter</a></li>
</ul>
<p><!-- Similar Posts took 3.666 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Playing With Zend Framework 1.7</title>
		<link>http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/</link>
		<comments>http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/#comments</comments>
		<pubDate>Wed, 03 Dec 2008 11:43:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.revolves.net/?p=17</guid>
		<description><![CDATA[I downloaded Zend Framework 1.7 a few days ago. So far, it looks like a great framework, backed by a good company. Previously, I learnt CakePHP 1.2 RC (I&#8217;m desparately waiting for 1.2 Final). CakePHP did have a good documentation, though lacking at some parts (especially the 1.2 version). But still, the usage for so [...]]]></description>
			<content:encoded><![CDATA[<p>I downloaded Zend Framework 1.7 a few days ago. So far, it looks like a great framework, backed by a good company.</p>
<p>Previously, I learnt CakePHP 1.2 RC (I&#8217;m desparately waiting for 1.2 Final). CakePHP did have a good documentation, though lacking at some parts (especially the 1.2 version). But still, the usage for so simple that you could, by yourself figure out many things. Code logically fitted into places.<span id="more-17"></span></p>
<p>Now, Zend Framework is a completely different monster. Instead of a large framework, its a collection of components, basically. You can use any component on its own, without using the entire library, as long as you also include dependencies. E.g., the Auth component would require Session component for storage, etc&#8230;</p>
<p>Zend Framework does have a good manual, covering practically all of its feature. You&#8217;d find some sort of code for all the components. But the problem is, in many cases, they are snippets. You may be able to use them if that&#8217;s the only component you&#8217;re ever going to use. But if you&#8217;re taking advantage of Zend Framework&#8217;s MVC structure, you might be clueless on where to put those codes, how to call them, when to call them etc.</p>
<p>If you&#8217;re just starting out with the Framework, I&#8217;d recommend going through the four screencasts. Then, watch the webinars on components of your interest, though they&#8217;re basically the same thing you&#8217;d see in the manual. But still, you can get some additional useful information in the webinars.</p>
<p>So far, I&#8217;ve been able to create the main index and bootstrap properly, along with understanding the significance of each statement I write. The AutoLoad feature of the framework is a real boon. You don&#8217;t need to scatter require or require_once statements all over your code. Zend Framework will load the class files automatically for you, if you follow the right convention (which you&#8217;ll learn in the manual and screencasts).</p>
<p>I even played with the Auth component, and it works like a charm. You can restrict access to certain controllers or even actions within controllers using Auth alone, but I&#8217;d always recommend using Acl for such things if you have more than 2 roles and need to restrict certain actions within controllers. It&#8217;ll save you a lot of typing in the end. A good tip to note here is that you can/should save the Acl roles/resources you&#8217;ve constructed in a cache (using Zend&#8217;s cache) or a file or in a database, so that you don&#8217;t need to have an overhead of constructing them everytime.</p>
<p>So far, I&#8217;ve been searching the internet for various tutorials on Zend tutorials. Many bloggers have posted tutorials on various sections of the framework, I&#8217;m assembling a list of tutorials I liked, and am soon going to post it here.</p>
<p>Well then, meet ya next time!<strong>Similar Posts:</strong>
<ul class="similar-posts">
<li><a href="http://www.revolves.net/2008/12/18/using-zend-acl-with-codeigniter/" rel="bookmark" title="December 18, 2008">Using Zend Acl With CodeIgniter</a></li>
<li><a href="http://www.revolves.net/2008/12/07/an-update-on-my-zend-framework-17-experience/" rel="bookmark" title="December 7, 2008">An Update On My Zend Framework 1.7 Experience</a></li>
</ul>
<p><!-- Similar Posts took 3.746 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.revolves.net/2008/12/03/playing-with-zend-framework-17/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.319 seconds -->
