2008年8月8日星期五

Re: [fw-mvc] Prevent action from being executed

-- Greg Freeman <greg@imagize.com.au> wrote
(on Thursday, 07 August 2008, 09:11 PM -0700):
> Every information article/book I've seen has used the action stack to
> do this so it is interesting you should say this matthew,

Those articles and books are not written by me. :)

> but it does make sense. Do you have any examples of using a view
> helper to do this? I like examples :)

Sure. I have the following helper that pulls my most recent bookmarks
from del.icio.us. The model in this case is a service class -- but it
could just as easily be a data table; the basic ideas are the same.

class Wopnet_View_Helper_Delicious
{
/**
* @var Zend_Cache
*/
public $cache;

/**
* @var Zend_Config
*/
public $config;

/**
* @var Zend_View_Interface
*/
public $view;

/**
* Set view
*
* @param Zend_View_Interface $view
* @return Wopnet_View_Helper_Delicious
*/
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
return $this;
}

/**
* Setup config and cache
*
* @return void
*/
public function __construct()
{
$config = Zend_Registry::get('siteConfig');
$this->config = $config->delicious;

$cacheOptions = array(
'caching' => ($this->config->caching) ? true : false,
'lifetime' => (int) $this->config->lifetime,
);
$backendOptions = array(
'cache_dir' => $config->vars->path->cache . 'delicious',
'file_name_prefix' => 'delicious'
);
$this->cache = Zend_Cache::factory('Core', 'File', $cacheOptions, $backendOptions);
}

/**
* Get list of recent del.icio.us bookmarks
*
* @return string
*/
public function delicious()
{
if (!$bookmarks = $this->cache->load('delicious')) {
$bookmarks = $this->getBookmarks();
$this->cache->save($bookmarks, 'delicious');
}

return $bookmarks;
}

/**
* Retrieve bookmarks as HTML list
*
* @return string
*/
public function getBookmarks()
{
$html = '';
require_once 'Zend/Service/Delicious.php';
try {
$delicious = new Zend_Service_Delicious($this->config->username, $this->config->password);
$deliciousPosts = $delicious->getRecentPosts(null, $this->config->limit);
foreach ($deliciousPosts as $post) {
$html .= '<a class="links" href="' . $post->getUrl() . '" target="_blank">' . $post->getTitle() . '</a>';
}
} catch (Exception $e) {
$html = '<span class="error">Error retrieving bookmarks from del.icio.us</span>';
}

return $html;
}
}

> Matthew Weier O'Phinney-3 wrote:
> > -- Greg Freeman <greg@imagize.com.au> wrote
> > (on Thursday, 07 August 2008, 04:49 PM -0700):
> > > How do you prevent an action from being called directly? I have an action
> > > that is used to render a menu response segment for the layout. I don't
> > > want
> > > people to be able to call it directly via a URL.
> >
> > Honestly, I'd recommend creating a view helper that queries the model
> > and prepares the content instead of using action(). action() is
> > expensive in terms of processing, and leads to issues just like the one
> > you're experiencing. :)

--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/

没有评论: