I'm working with cakephp 2.x
I've got a site with static pages (Multilanguage) and cacheAction.
The cache does not recognize the language and caches the page in the firest language....
Any idea how to solve (a part from disabling the cache?)
Thanks,
Massimo
class PagesController extends AppController {
/**
* This controller does not use a model
*
* #var array
*/
public $uses = array();
public $helpers = ['Cache','AbTest.AbTest'];
public $cacheAction = '1 month';
public $components = array('AbTest.AbTest');
....
if ($locale && file_exists(APP . 'View' . $theme_path . DS . $this->viewPath . DS . $locale .DS. implode('/', $path) . $this->ext ))
{
array_unshift($path,$locale);
}
try {
$this->render(implode('/', $path));
} catch (MissingViewException $e) {
if (Configure::read('debug')) {
throw $e;
}
throw new NotFoundException();
}
I expect the cache to present me ita/pages/who and eng/pages/who as different pages, while it outputs alway ita/pages/who
There is an option for this called Cache.viewPrefix. You didn't mention how you manage the languages in your site, but the basic logic is that you can set the cache file prefix with the language and this way you will have separate cache files for every language. For example, you can do it in your PagesController or AppController like
Configure::write('Cache.viewPrefix', /* place the language here */);
Related
I am using cakephp 2.6.7. I want to integrates Authorize Api(http://www.authorize.net/) to my cakephp app. I set up api by composer in my localhost. It works fine. Here is the file structure:
Here charge-credit-card.php is the final script to run. Inside this file I include some pre requisite files as follows:
require 'authorize/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
Now I put the authorize folder inside Vendor folder of cakephp:
And I add autoload.php in paymentsController.php as follows:
require_once(APP . 'Vendor' . DS . 'authorize' . DS . 'autoload.php');
But I am confused about
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
Replacement. How should I replace these two lines inside cakephp controller? Here is my full code:
<?php
require_once(APP . 'Vendor' . DS . 'authorize' . DS . 'autoload.php');
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
define("AUTHORIZENET_LOG_FILE", "phplog");
class PaymentsController extends AppController {
var $layout = 'admin';
// public $components = array('Auth');
public function isAuthorized($user = null) {
$sidebar = $user['Role']['name'];
$this->set(compact('sidebar'));
return true;
}
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('process');
}
}
?>
Thanks for your time.
I guess your question is specific to php aliases and not the ANet sdk itself?
Here's a question about making globally accessible aliases. There's no straight forward way do it, it seems.
PHP Global namespace aliases
Related:
http://grokbase.com/t/php/php-general/10a38hape9/is-it-possible-to-create-a-global-namespace-alias
The default approach is to define ...
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
... in each of the files where you need to use the aliases. (As you must know, once autoloaded, the namespaces are globally accessible)
That should have your application working.
I want to create an "Admin Panel/Interface" in one of my CakePHP projects. You know its very common in modern web sites. At first, I planned to create a plugin for this, and tried to do it. It didn't work, no idea why, I'll ask for help for it later. But, next I saw that CakePHP already provides this feature, using "Scafolding". I am now trying this, but don't know why its not working as I expected. Here is what I did :
app/config/core.php :
---------------------
.
.
.
Configure::write('Routing.prefixes',array('admin'));
.
.
.
app/Controller/AppController.php :
----------------------------------
.
.
.
public $components=array(
'Session',
'Auth'=>array(
'loginRedirect'=>array('admin'=>true,'controller'=>'home','action'=>'index'),
'logoutRedirect'=>array('controller'=>'home','action'=>'index'),
'authorize'=>array('Controller')
)
);
.
.
.
I thought, there should be a seperate controller for Admin Panel, that's why I created it :
app/Controller/AdminsController.php :
-------------------------------------
<?php
App::uses('AppController','Controller');
class AdminsController extends AppController{
public $name='Admins';
public $scaffold='admin';
}
But it didn't work. So, I thought CakePHP provided this feature for all individual controller; I mean, I thought I am supposed to have the Admin Panel for all individual controller, not as a different module/controller/sub-system. So, I changed a little one of my existing controllers "Controllers1" :
<?php
App::uses('AppController','Controller');
class Controllers1Controller extends AppController{
public $name='Controllers1';
public $scaffold='admin';
}
then tried to go to this URL : my_site/admin/jobs/view
but still same result.
Please give me a suggestion, what should I do ? Should I create a new plugin for the "Admin Panel", or Scafolding is better ? And what is my fault ?
Thanks
The AdminsController is not necessary to use the admin prefix, all you need to do is define the Routing.Prefixes like you already did.
Configure::write('Routing.prefixes',array('admin'));
For the JobsController example that you mentioned to us what all you need to do to make it work is:
<?php
App::uses('AppController', 'Controller');
class JobsController extends AppController {
public $scaffold = 'admin';
}
Because the way to make Routing Prefixes work is to declare methods with the prefixes, not use an additional controller:
<?php
App::uses('AppController', 'Controller');
class ArticlesController extends AppController {
function admin_index() {
//This method can be found under /admin/articles
}
}
I tried all the suggested solution for this problem which is clearing your cache folders. I also disable the cache Configure::write('Cache.disable', true); My debug level is set to 2. But I always get a same error. The table that is missing is only new added table. By the way, I'm only running in my localhost.
this is my model named Department.php
<?php
class Department extends AppModel {
public $name = 'Department';
}
?>
this is my controller name DepartmentsController.php
<?php
class DepartmentsController extends AppController {
public $name = 'Departments';
public $helpers = array('Html', 'Form','Session');
public $components = array('RequestHandler','Session');
function index() {
$this->Department->recursive = 0;
$this->set('departments', $this->paginate());
}
}
?>
Advance thank you for the answer! :D
Assuming you are actually using a database I would try the following things:
If your database table isn't named using the cakephp conventions, manually set it:
public $useTable = 'table_name';
Make sure your database config is using the right database in app/config/database.php
If that model is in a plugin, use the plugin syntax when including it in the controller:
public $uses=array('PluginName.Department');
I've get this message when I tried to use a wrong controller and I figured it out that I'm not getting the right Error from cakephp I've got in 2.0.0 the right one:
Now when I try a wrong controller I get only this message:
Fatal error: Call to a member function Flash() on a non-object in
/srv/www/htdocs/web843/HTML/schaetzmal/lib/Cake/View/Layouts/default.ctp
on line 44
Does cakephp 2.0.3 have an bug or do I miss something to install to let work this or something else what I can do?
Make sure you've added the Session helper to your public $helpers array.
class SomethingsController extends AppController {
public $helpers = array('Session');
}
Or you could add it to a global AppController so that the Session helper is available to all controllers.
class AppController extends Controller {
public $helpers = array('Session');
}
I found some problems why my AppController didn´t work.
Like mensch says i have to use Session in my AppController for global but that´s not the solution because the book of cakephp says in "a global AppController"
NOTE
CakePHP merges the following variables from the AppController to your application’s controllers:
$components
$helpers
$uses
but it´s not happening. Because i overwrite it in the public variable $helpers.
therfore i take the parent given one and merge it with it:
<?
class AppController extends Controller {
public $viewClass = 'Theme';
public $theme;
public function beforeFilter() {
parent::beforeFilter();
$this->theme = 'SM';
$this->helpers = array('Form','Html','Js');
}
}
?>
the $this->helpers = array('Form','Html','Js'); do the merge and it works fine.
thank you guys
thanks for helping mensch that was the hack i needed
I'm new to cakePhp development. I've stuck on following problem:
I've made few models, controllers and views - it works great. The problem is that after production, I have to made new table(Transactionaltemp table and corresponding model and controller ) in the db that logically is "connected" to other tables, but technically does not needs to - for ex. it holds temporary info on user_id, time, ip and similar. So, other tables doesn't need to be directly connected to that.
The problem is when I try (in some other controller than transactionaltemps_controller):
$this->loadModel('Transactionaltemp');
I get error - the model is not found (it is true because the model is missing in the cache). Interesting enough transactionaltempls_controller is in the cache (in the cake_controllers_list file).
I tried following stuff to resolve the problem:
clear cache
disable cache
tried using uses={..} code in the controller that I would like to use mymodels_controller
tried using init('Transactionaltemp')
with no success. Here is corresponding code:
The model:
<?php
class Transactionaltemp extends AppModel
{
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
?>
The controller:
<?php
class TransactionaltempsController extends AppController
{
var $name = 'Transactionaltemps';
var $scaffold;
}
?>
I'll very grateful to any help!!!
App:Import('Model','Transactionaltemp');
$this->Transactionaltemp= new Transactionaltemp;
I hope this may work
If you are connecting to a table name with different name than your model, you must specify the table name in it:
<?php
class Transactionaltemp extends AppModel
{
var $uses = 'Transactional';
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
Try
App::Import('Model', 'YourModelName');
in your controller (or where you want).