Sometimes, some person, somethings, you feel dispointed, feel down… anything can frighten or block me as a protector?
Sometimes, some person, somethings, you feel dispointed, feel down… anything can frighten or block me as a protector?
It’s the string cutting helper which deal with both UTF8 and others encodings. Also it remove html tags by default.
[codesyntax lang=”php” lines=”no”]
/**
* Takes a string and optionally a maximum length and 'cut' the string to match that length
* based on words and not on characters.
*
* @author kim
*/
class App_View_Helper_Cut extends Zend_View_Helper_Abstract
{
/**
* do string cut
*
* @param string $pStr - The string to be cut
* @param intefer $pMaxLen - The maximum length cut
* @return string
*/
public function cut($pStr, $pMaxLen = 40)
{
// filter all the tags
$filter = new Zend_Filter_StripTags();
$pStr = trim($filter->filter($pStr));
$returnStr = $this->cutstr($pStr, $pMaxLen, '...');
return $returnStr;
}
/**
* cut string, utf8 by default
*
* @param $string - string to cut
* @param $length - Max length to cut
* @param $dot - the tail added to the sub-string
* @param $encoding - the encoding of string
* @return string
*/
public function cutstr($string, $length, $dot = '', $encoding = 'utf8')
{
if (strlen($string) <= $length) {
return $string;
}
$strcut = '';
if (strtolower($encoding) == 'utf8') {
$n = $tn = $noc = 0;
while ($n < strlen($string)) {
$t = ord($string[$n]);
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t < 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if ($noc >= $length) {
break;
}
}
if ($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length - strlen($dot) - 1; $i++) {
if (ord($string[$i]) > 127) {
$strcut .= $string[$i] . $string[++$i];
} else {
$strcut .= $string[$i];
}
}
}
return $strcut . $dot;
}
}
[/codesyntax]
To create a cron job script in Zend Framework, we just need 3 steps :
1. Create a new directory called “scripts” which is the storage of all the scripts(.sh, .php, etc.).
2. Copy public/index.php into “scripts” and rename it to your cron job name, for example “cron.php”.
3. Modify the code. Instead running the application, we now only do the bootstrap:
[codesyntax lang=”php” lines=”no”]
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'development'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
date_default_timezone_set('America/New_York');
/** Zend_Application */
require_once 'Zend/Application.php';
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
// only do bootstrap
//$application->bootstrap()->run();
$application->bootstrap();
// get the options and run CLI
try {
$opts = new Zend_Console_Getopt('abc:');
if (isset($opts->a)) {
echo "I got the a option.\n";
}
if (isset($opts->b)) {
echo "I got the b option.\n";
}
if (isset($opts->c)) {
echo "I got the c option.\n";
}
} catch (Zend_Console_Getopt_Exception $e) {
echo $e->getUsageMessage();
exit;
} catch (exception $e) {
echo $e->getMessage();
exit;
}
[/codesyntax]
“ZFDebug is a plugin for the Zend Framework for PHP5, providing useful debug information displayed in a small bar at the bottom of every page.”
We just need two steps to setup ZFDebug:
1. Download it from https://github.com/jokkedk/ZFDebug, place it inside library just next to Zend Framework
2. Modify application/Bootstrap.php to add initialized function:
[codesyntax lang=”php” lines=”no” container=”div”]
/**
* include ZFDebug console in development environment
*/
protected function _initZFDebug()
{
// normally only section [development] has the "zfdebug" option
if ($this->getOption('zfdebug')) {
// namespace "ZFDebug" for autoloader
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('ZFDebug');
// initialize front controller
$this->bootstrap('FrontController');
$front = $this->getResource('FrontController');
// enable zfdebug options
$options = array('plugins' => array(
'Variables',
'Memory',
'Time',
'Registry',
'Exception',
));
// add caching backend option if specified
if ($this->hasPluginResource('cache')) {
$this->bootstrap('cache');
$cache = $this->getPluginResource('cache')->getDbAdapter();
$options['plugins']['Cache']['backend'] = $cache->getBackend();
}
// add db option if specified
if ($this->hasPluginResource('db')) {
$this->bootstrap('db');
$db = $this->getPluginResource('db')->getDbAdapter();
$options['plugins']['Database']['adapter'] = $db;
}
// register ZFDebug with front controller
$zfdebug = new ZFDebug_Controller_Plugin_Debug($options);
$front->registerPlugin($zfdebug);
}
}
[/codesyntax]
And that’s all, now you should see a debug bar just at the bottom of your project like this:
When we try to generate the project by Zend_Tool by command line like this:
[codesyntax lang=”bash” lines=”no”]
$ zf create project myProject
[/codesyntax]
We get a standard structure which is recommended by Zend Framework:
It works very fine as “one” website project. But how about if we want to create multiple websites without rewriting too many code?
The main idea is to share as much as possible the libraries meanwhile keeping independence of each website, especially the designs/templates/publics.
Here is one simple way which makes the minimal modifications and satisfy our needs:
(1) Move the folder “library” out of the project.
(2) Modify /public/index.php as below to make sure the library/ is in include paths:
[codesyntax lang=”php” lines=”no”]
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
//realpath(APPLICATION_PATH . '/../library'),
realpath(APPLICATION_PATH . '/../../library'),
get_include_path(),
)));
[/codesyntax]
(3) Modify /application/config/application.ini :
[codesyntax lang=”ini” lines=”no”]
;includePaths.library = APPLICATION_PATH "/../library" includePaths.library = APPLICATION_PATH "/../../library"
[/codesyntax]
Assume that we have project1, project2 and project3, all applied the same treatment. And finally the websites could like this:
In the picture above, assume that our global namespace is “Projlib”. We have a model class Projlib/Model/Foo.php
[codesyntax lang=”php” lines=”no”]
class Projlib_Model_Foo
{
// ...
}
[/codesyntax]
Then we can just put this configuration inside application/configs/application.ini to make it autoloaded:
[codesyntax lang=”ini” lines=”no”]
; --- my library prefix ----------------------------------------------------------------- autoloadernamespaces.global = "Projlib_"
[/codesyntax]
And that’s all! Enjoy it.