How To Use Multiple Cache Folder In CodeIgniter - php

I have a multiple language website I want to create a specific cache folder for every language. How Can I do this?
I currently using one cache folder with this code.
Can You Help Me?
$lang = $CI->session->userdata('language');
$cache_path .= md5($uri).'-'.$lang;

the parameter that configures the cache folder is in config.php and its called cache_path. For modifying in during runtime, we can use the $this->config->set_item function. Obviously, the cache folder switch should be done as early as possible in the controller function, before the caching function call.
Here is a sample implementation, controller Test:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
$user_language = 'french';
$this->config->set_item('cache_path', 'IS_ROOT/cache/' . $user_language . '/');
$this->output->cache(1);
$this->load->view('welcome_message');
}
}
when you run it, you can see in the logs, that the directory "french" under the "global" IS_ROOT/cache was used:
INFO - 2018-06-09 22:12:39 --> File loaded: /php_basedir/CodeIgniter_3_1_8/application/views/welcome_message.php
DEBUG - 2018-06-09 22:12:39 --> Cache file written: IS_ROOT/cache/french/bc3ad60292ed776397da07cac67ddd28
INFO - 2018-06-09 22:12:39 --> Final output sent to browser
DEBUG - 2018-06-09 22:12:39 --> Total execution time: 0.0138
hope it helps

I Find a solution. we must edit output.php in core folder like this.
public function _write_cache($output){
$CI =& get_instance();
$lang = $CI->session->userdata('language');
$path = $CI->config->item('cache_path');
$cache_path = ($path === '') ? APPPATH.'cache_'.$lang.'/' : $path;
//other code
}
public function _display_cache(&$CFG, &$URI){
$CI =& get_instance();
$lang = $CI->session->userdata('language');
$cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache_'.$lang.'/' : $CFG->item('cache_path');
//other code
}

Related

Laravel in a subfolder and pagination

I've my Laravel project in a sub (sub) folder from my root folder and am using the simplePaginate() method in some views. After a little search I've noticted the AbstractPaginator is used and provides a method url() which is somewhere down the road invoked by the BootstrapThreeNextPreviousButtonRendererTrait which gets called from SimpleBootstrapThreePresenter.
I've been searching in the my config/app.php and helpers.php file to find something pointing to a solution. But haven't found anything yet.
How can I set up Laravel (5.1) to use my subfolder structure with the pagination class?
I've solved it modifying the BootstrapThreeNextPreviousButtonRendererTrait. I'm aware I could have also modified the AbstractPaginator but since I'm not overseeing the consequences of that right now I've chosen to tailor the trait to my needs, like so:
<?php
namespace Illuminate\Pagination;
use Illuminate\Support\Facades\Request;
trait BootstrapThreeNextPreviousButtonRendererTrait
{
/**
* Get the previous page pagination element.
*
* #param string $text
* #return string
*/
public function getPreviousButton($text = '«')
{
// If the current page is less than or equal to one, it means we can't go any
// further back in the pages, so we will render a disabled previous button
// when that is the case. Otherwise, we will give it an active "status".
if ($this->paginator->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = url() . '/' . Request::path() . '?page=' . ($this->paginator->currentPage() - 1);
//Laravel shipped code disabled because of an installation in a sub-sub folder.
//$url = $this->paginator->url(
// $this->paginator->currentPage() - 1
//);
return $this->getPageLinkWrapper($url, $text, 'prev');
}
/**
* Get the next page pagination element.
*
* #param string $text
* #return string
*/
public function getNextButton($text = '»')
{
// If the current page is greater than or equal to the last page, it means we
// can't go any further into the pages, as we're already on this last page
// that is available, so we will make it the "next" link style disabled.
if (! $this->paginator->hasMorePages()) {
return $this->getDisabledTextWrapper($text);
}
$url = url() . '/' . Request::path() . '?page=' . ($this->paginator->currentPage() + 1);
//Laravel shipped code disabled because of an installation in a sub-sub folder.
//$url = $this->paginator->url($this->paginator->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text, 'next');
}
}

How to Read/Render URLs Like Zend Framework

I am working on creating my own framework in php.
Everything is going as planned except working the framework according to the URL.
I cannot understand this:
Normal URL loading from www.mydomain.com/folderone/foldertwo/index.php
Zend Framework URL loading from the same URL would be
www.mydomain.com/folderone(controller)/folder2(action)/variables
how can i create that logic?
What am i missing?
I am really dedicated to create this framework.
I had the same task as I setup my framework. This is a solution work for me.
Create first your .htaccess file. Set up your rewrite conditions and exclude your template path. You can do it like that (just copy & paste):
RewriteEngine On
Options +Indexes
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/index\.php -f
RewriteCond %{DOCUMENT_ROOT}/template -d
RewriteRule ^(.*)$ index\.php?$1 [QSA]
Before I go ahead, we have to create five directories at least:
/var/www/models/
/var/www/controllers/
/var/www/classes/
/var/www/system/
/var/www/template/
Now, I added an auto loading in my index.php:
<?php
error_reporting(E_ALL ^ E_NOTICE);
session_start();
function autoload($class_name)
{
$autoloadDirs = array('models', 'classes', 'controllers');
foreach($autoloadDirs as $dir)
{
if(file_exists($dir.'/'.$class_name.'.php'))
{
require_once($dir.'/'.$class_name.'.php');
}
}
}
spl_autoload_register('autoload');
require_once('system/Calling.php');
Calling::Run();
?>
At the script above you'll see that require_once within Calling.php
class Calling
{
public static function Run($querystring = null)
{
//1. Parameter = Contollername
//2. Parameter = Action
$qString = preg_replace('/(\/$|^\/)/','',$querystring === null ? $_SERVER['QUERY_STRING'] : $querystring);
$callParam = !empty($qString) ? explode('/', $qString) : array();
$controllerName = count($callParam) > 0 ? (class_exists(ucfirst($callParam[0]).'Controller') ? ucfirst(array_shift($callParam)) : 'Error') : 'Main';
//All controllers have suffix "Controller" -> NameController.php
//and class name ike NameController
//If no controller name given, use MainController.php
$controllerClassName = $controllerName.'Controller';
//All public methods have suffix "Action" -> myMethodAction
//If there is no method named, use DefaultAction
$actionName = count($callParam) > 0 && method_exists($controllerClassName, ucfirst($callParam[0]).'Action') ? ucfirst(array_shift($callParam)) : 'Default';
$actionFunctionName = $actionName.'Action';
//Fetch the params
$param = new stdClass();
for($i = 0; $i < count($callParam); $i += 2)
{
$param->{$callParam[$i]} = isset($callParam[$i + 1]) ? $callParam[$i+1] : null;
}
////////////////////////////////////////////////////////////
//Init the Controller
$controller = new $controllerClassName($controllerName, $actionName);
$controller->$actionFunctionName($param);
////////////////////////////////////////////////////////////
//If you adapt this code: Is up to you to extends your controller
//from an internal controller which has the method Display();
$controller->Display();
}
}
Further, in your controller directory add your first controller namend MainController.php
//--> just better if you have also an internal controller with your global stuff
//--> class MainController extends Controller
class MainController
{
/** This is the default action
* #param $params
* #access public
* #return
*/
public function DefaultAction(stdClass $params)
{
//-> Do your staff here
}
/** This is the second action
* #param $params
* #access public
* #return
*/
public function SecondAction(stdClass $params)
{
//-> Do your staff here
}
/** This is the view handling method which has to run at least
* and I recommend to set up an internal controller and to extend
* all other controller from it and include this method in your
* internal controller
* #param
* #access
* #return
*/
public function Display()
{
//-> Run your template here
}
?>
Now, you can call the controller, methods and params like that:
//-> Load the main controller with default action
www.example.com/
//-> Load the main controller with default action
www.example.com/main/default/
//-> Load the main controller with second action
www.example.com/main/second/
//-> Load the main controller with second action and gives two params
www.example.com/main/second/key1/value1/key2/value2/
And now you'll have the following files and directories to start up your own framework.
/var/www/.htaccess
/var/www/index.php
/var/www/controllers/MainController.php
/var/www/system/Calling.php
/var/www/models/
/var/www/classes/
/var/www/template/
Enjoy your new basic framework kit
Zend FrameWork and most MVC frameworks use a BootStrap.
It routes the URL (using .htaccess) to one file let's say (index.php) by using something like that:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
Which loads the bootstrap and the routing class that takes whatever data it needs from the URL.
According to ZEND manual:
Routing is the process of taking a URI endpoint (that part of the URI
which comes after the base URL) and decomposing it into parameters to
determine which module, controller, and action of that controller
should receive the request. This values of the module, controller,
action and other parameters.
Routing occurs only once: when
the request is initially received and before the first controller is
dispatched.
EDIT: Answer for your comment:
Far away from ZEND FRAMEWORK, that's a code to test in a new PHP file:
<?php
$url = 'http://test.com/test1/test2/news.php';
$parse = parse_url($url);
$tokens = explode("/", $parse[path]);
print_r($tokens);
?>
If you want to build a router, you could look at Glue PHP for giving you examples.
It's a micro-framework that just do the routing part.

Problems up setting up codeigniter with sparks & active record

I am having problems getting my sparks install to work with my codeigniter install
here are the steps I took which were as follows:
I went to my root of my codeigniter project folder through my PHPCLI to install sparks using the below command
php -r "$(curl -fsSL http://getsparks.org/go-sparks)"
i installed the active record library using the spark command below
php tools\spark install -v0.0.2 php-activerecord
2a. this command gave me the follwing folder structure
-application
-sparks
-php-activerecord
-0.0.2
-config
-variables
-vendor
-system
-tests
-tools
-lib
-spark
-sparktypes
-test
-user_guide
2b. this command generates a sparks containing php active record folder that makeup the necessary components of php-activerecord, the command using generates a MY_Loader.php file which looks like this
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Sparks
*
* An open source application development framework for PHP 5.1.6 or newer
*
* #package CodeIgniter
* #author CodeIgniter Reactor Dev Team
* #author Kenny Katzgrau <katzgrau#gmail.com>
* #since CodeIgniter Version 1.0
* #filesource
*/
/**
* Loader Class
*
* Loads views and files
*
* #package CodeIgniter
* #subpackage Libraries
* #author CodeIgniter Reactor Dev Team
* #author Kenny Katzgrau <katzgrau#gmail.com>
* #category Loader
* #link http://codeigniter.com/user_guide/libraries/loader.html
*/
class MY_Loader extends CI_Loader
{
/**
* Keep track of which sparks are loaded. This will come in handy for being
* speedy about loading files later.
*
* #var array
*/
var $_ci_loaded_sparks = array();
/**
* Is this version less than CI 2.1.0? If so, accomodate
* #bubbafoley's world-destroying change at: http://bit.ly/sIqR7H
* #var bool
*/
var $_is_lt_210 = false;
/**
* Constructor. Define SPARKPATH if it doesn't exist, initialize parent
*/
function __construct()
{
if(!defined('SPARKPATH'))
{
define('SPARKPATH', 'sparks/');
}
$this->_is_lt_210 = (is_callable(array('CI_Loader', 'ci_autoloader'))
|| is_callable(array('CI_Loader', '_ci_autoloader')));
parent::__construct();
}
/**
* To accomodate CI 2.1.0, we override the initialize() method instead of
* the ci_autoloader() method. Once sparks is integrated into CI, we
* can avoid the awkward version-specific logic.
* #return Loader
*/
function initialize()
{
parent::initialize();
if(!$this->_is_lt_210)
{
$this->ci_autoloader();
}
return $this;
}
/**
* Load a spark by it's path within the sparks directory defined by
* SPARKPATH, such as 'markdown/1.0'
* #param string $spark The spark path withint he sparks directory
* #param <type> $autoload An optional array of items to autoload
* in the format of:
* array (
* 'helper' => array('somehelper')
* )
* #return <type>
*/
function spark($spark, $autoload = array())
{
if(is_array($spark))
{
foreach($spark as $s)
{
$this->spark($s);
}
}
$spark = ltrim($spark, '/');
$spark = rtrim($spark, '/');
$spark_path = SPARKPATH . $spark . '/';
$parts = explode('/', $spark);
$spark_slug = strtolower($parts[0]);
# If we've already loaded this spark, bail
if(array_key_exists($spark_slug, $this->_ci_loaded_sparks))
{
return true;
}
# Check that it exists. CI Doesn't check package existence by itself
if(!file_exists($spark_path))
{
show_error("Cannot find spark path at $spark_path");
}
if(count($parts) == 2)
{
$this->_ci_loaded_sparks[$spark_slug] = $spark;
}
$this->add_package_path($spark_path);
foreach($autoload as $type => $read)
{
if($type == 'library')
$this->library($read);
elseif($type == 'model')
$this->model($read);
elseif($type == 'config')
$this->config($read);
elseif($type == 'helper')
$this->helper($read);
elseif($type == 'view')
$this->view($read);
else
show_error ("Could not autoload object of type '$type' ($read) for spark $spark");
}
// Looks for a spark's specific autoloader
$this->ci_autoloader($spark_path);
return true;
}
/**
* Pre-CI 2.0.3 method for backward compatility.
*
* #param null $basepath
* #return void
*/
function _ci_autoloader($basepath = NULL)
{
$this->ci_autoloader($basepath);
}
/**
* Specific Autoloader (99% ripped from the parent)
*
* The config/autoload.php file contains an array that permits sub-systems,
* libraries, and helpers to be loaded automatically.
*
* #param array|null $basepath
* #return void
*/
function ci_autoloader($basepath = NULL)
{
if($basepath !== NULL)
{
$autoload_path = $basepath.'config/autoload'.EXT;
}
else
{
$autoload_path = APPPATH.'config/autoload'.EXT;
}
if(! file_exists($autoload_path))
{
return FALSE;
}
include($autoload_path);
if ( ! isset($autoload))
{
return FALSE;
}
if($this->_is_lt_210 || $basepath !== NULL)
{
// Autoload packages
if (isset($autoload['packages']))
{
foreach ($autoload['packages'] as $package_path)
{
$this->add_package_path($package_path);
}
}
}
// Autoload sparks
if (isset($autoload['sparks']))
{
foreach ($autoload['sparks'] as $spark)
{
$this->spark($spark);
}
}
if($this->_is_lt_210 || $basepath !== NULL)
{
if (isset($autoload['config']))
{
// Load any custom config file
if (count($autoload['config']) > 0)
{
$CI =& get_instance();
foreach ($autoload['config'] as $key => $val)
{
$CI->config->load($val);
}
}
}
// Autoload helpers and languages
foreach (array('helper', 'language') as $type)
{
if (isset($autoload[$type]) AND count($autoload[$type]) > 0)
{
$this->$type($autoload[$type]);
}
}
// A little tweak to remain backward compatible
// The $autoload['core'] item was deprecated
if ( ! isset($autoload['libraries']) AND isset($autoload['core']))
{
$autoload['libraries'] = $autoload['core'];
}
// Load libraries
if (isset($autoload['libraries']) AND count($autoload['libraries']) > 0)
{
// Load the database driver.
if (in_array('database', $autoload['libraries']))
{
$this->database();
$autoload['libraries'] = array_diff($autoload['libraries'], array('database'));
}
// Load all other libraries
foreach ($autoload['libraries'] as $item)
{
$this->library($item);
}
}
// Autoload models
if (isset($autoload['model']))
{
$this->model($autoload['model']);
}
}
}
}
i modify my autoload.php to include php active record as below
$autoload['sparks'] = array('php-activerecord/0.0.2');
when i run my codeigniter site i get the following error
A PHP Error was encountered
Severity: Notice
Message: Use of undefined constant EXT - assumed 'EXT'
Filename: core/MY_Loader.php
Line Number: 174
Backtrace:
File: C:\xampp\htdocs\orm\application\core\MY_Loader.php
Line: 174
Function: _exception_handler
File: C:\xampp\htdocs\orm\application\core\MY_Loader.php
Line: 154
Function: ci_autoloader
File: C:\xampp\htdocs\orm\application\core\MY_Loader.php
Line: 67
Function: initialize
File: C:\xampp\htdocs\orm\index.php
Line: 274
Function: require_once
I am curious as to what could be causing this error? Please let me know if there is any other configuration that I am missing or if theres a mistake that I have made.
The EXT is defined in your root index.php file.
// The PHP file extension
// this global constant is deprecated.
define('EXT', '.php');
See if it's still there or not?
IN case that constant is not there, you can also define the autoload.php without using the constant.
I know this is an old post but hopefully this will save someone some time... I had the same issue and the above fix did not work. I finally fixed the issue in the htaccess file by removing the RewriteBase although I am not sure why this caused the issue in the first place.
just commented out the line as follows:
RewriteBase /
to
#RewriteBase /

CodeIgniter 2 + Zend 2 library barcode

Problem: rendering barcodes in CodeIgniter via Zend library barcode.
I googled, and also tried all tutorials on first 2 pages. I stackoverflowed and found quiet a few topics on my problem, even few are marked as answered but no luck.
Finally I tried this https://stackoverflow.com/a/15480779/1564365 but yet another error message.
error:
Fatal error: Class 'Zend\Barcode\ObjectPluginManager' not found
that means it is actually loading Barcode library but with error.
sidenote: ZF 2.2 fresh download (today), CI 2.1.3 fresh download (today)
To solve this, I am forced to use ZF1.
step by step:
Download (Zend Framework 1.12.3 Full) from here
Unzip files and locate folder Zend in ./libraries folder copy it to CI application/libraries
Create new file inside (CI) application/libraries/Zend.php "loader for ZF"
with code as follows
<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
/**
* Zend Framework Loader
*
* Put the 'Zend' folder (unpacked from the Zend Framework package, under 'Library')
* in CI installation's 'application/libraries' folder
* You can put it elsewhere but remember to alter the script accordingly
*
* Usage:
* 1) $this->load->library('zend', 'Zend/Package/Name');
* or
* 2) $this->load->library('zend');
* then $this->zend->load('Zend/Package/Name');
*
* * the second usage is useful for autoloading the Zend Framework library
* * Zend/Package/Name does not need the '.php' at the end
*/
class CI_Zend
{
/**
* Constructor
*
* #param string $class class name
*/
function __construct($class = NULL)
{
// include path for Zend Framework
// alter it accordingly if you have put the 'Zend' folder elsewhere
ini_set('include_path',
ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries');
if ($class)
{
require_once (string) $class . EXT;
log_message('debug', "Zend Class $class Loaded");
}
else
{
log_message('debug', "Zend Class Initialized");
}
}
/**
* Zend Class Loader
*
* #param string $class class name
*/
function load($class)
{
require_once (string) $class . EXT;
log_message('debug', "Zend Class $class Loaded");
}
}
and controllers method should be as follows
function barcode() {
$this->load->library('zend');
$this->zend->load('Zend/Barcode');
$test = Zend_Barcode::draw('ean8', 'image', array('text' => '1234565'), array());
var_dump($test);
imagejpeg($test, 'barcode.jpg', 100);
}

gettext problem in codeigniter

I added one library for gettext translation. Added corresponding po and mo files.
And the translation is working fine.
Now when I update my po file, change some translation.. after that when I reload the page, I am getting the old translation, not the new.
Here is the Library code:
/**
* This method overides the original load method. Its duty is loading the domain files by config or by default internal settings.
*
*/
function load_gettext($userlang = false) {
/* I want the super object */
if ($userlang)
$this->gettext_language = $userlang;
else
$this->gettext_language = 'it_IT';
log_message('debug', 'Gettext Class gettext_language was set by parameter:' . $this->gettext_language);
putenv("LANG=$this->gettext_language");
setlocale(LC_ALL, $this->gettext_language);
/* Let's set the path of .po files */
$this->gettext_path = APPPATH . 'language/locale';
log_message('debug', 'Gettext Class path chosen is: ' . $this->gettext_path);
bindtextdomain($this->gettext_domain, $this->gettext_path);
textdomain($this->gettext_domain);
log_message('debug', 'Gettext Class the domain chosen is: ' . $this->gettext_domain);
return true;
}
/**
* Plural forms added by Tchinkatchuk
* http://www.codeigniter.com/forums/viewthread/2168/
*/
/**
* The translator method
*
* #param string $original the original string to translate
* #param array $aParams the plural parameters
* #return the string translated
*/
function _trans($original, $aParams = false) {
if (isset($aParams['plural']) && isset($aParams['count'])) {
$sTranslate = ngettext($original, $aParams['plural'], $aParams['count']);
$sTranslate = $this->replaceDynamically($sTranslate, $aParams);
} else {
$sTranslate = gettext($original);
if (is_array($aParams) && count($aParams))
$sTranslate = $this->replaceDynamically($sTranslate, $aParams);
}
return $sTranslate;
}
This is the usage in a controller:
$this->pos_language->load_gettext('fr_FR');
echo $this->pos_language->_trans('Hello world, good morning');
I think you need to compile your .po files to .mo files. Gettext uses the .mo file, the .po is just a human readable form.
If you haven't done the compilation step, your application is still reading your old .mo files, with the untranslated strings...
This page has some more info about gettext translation: http://wiki.creativecommons.org/Translating_PO_Files
I wrote a helper to integrate php-gettext with Smarty & Code Igniter 2
http://bit.ly/rrITVx
I hope it might help

Categories