CakePHP won't read my default.po file - cakephp

I've got a CakePHP application that works with multiple languages (on database level), and in the end, I generated a .po file with PoEdit and translated the keywords, but cake doesn't want to read it.
This is my code in AppController:
Configure::write('Config.language', 'mkd');
class AppController extends Controller {
// ... some code here ...
public function beforeFilter () {
parent::beforeFilter();
$this->params['language'] =
!$this->params['language'] ? 'mkd' : $this->params['language'];
Configure::write('Config.language', $this->params['language']);
// more code...
}
I know that declaring the language before AppController class is obsolete, but it was an act of desperation. Both parameters, $this->params['language'] and Configure::read('Config.language'), are set to 'mkd', however it only displays the original values.
This is my localization folder structure:
C:\wamp\www\animalmedica\app\Locale\mkd\LC_MESSAGES\default.po
I am using CakePHP 2.3. What am I doing wrong here?

Related

CakePHP update public $uses = false; in action from same Controller

I'm writing an installation script for my CakePHP web application. I have a InstallController with 6 actions: step1, step2, step3, etc.
At step1 I'm handling Config/database.php creation. Because this file is empty and no datasource is available I have to set public $uses = false; in the InstallController.
At step2 the Config/database.php file is set so I should be able to make a connection to the datasource. This is also necessary because I want to update some database fields in the following steps.
Is it possible to update the public $uses = false; in every following steps after step1?
I'm using CakePHP version 2.3.5
Have you considered loading the model within the actions? So, something like:
<?php
App::uses('AppController', 'Controller');
class InstallController extends AppController {
public $uses = false;
public function step1() {
}
public function step2() {
$this->loadModel("Install");
$this->Install->callMethod();
}
}
In CakePHP 2.x models are lazy loaded, so as long as your step1 action doesn't try to make use of a model, you can safely declare the models in your controllers $uses property, they are not being constructed until your code actually makes use of them.
However, if for some reason you'd actually need to modify $uses, well then just do it, as mentioned models are lazy loaded, so you can modify $uses whenever you want and then access the models afterwards via magic properties on the controller.

Admin Panel/Interface in a CakePHP project

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
}
}

CakePHP 2.3 adding new folder to load models not working

I'm using the following code in my bootstrap.php (as explained here) to load models also from another folder:
App::build(array('Model' => array('/my/path/to/models')));
This seems to work. I have a model MyModel inside that folder, which I include in the controller I want to use it like usually:
var $uses = array('MyModel');
If I print App::objects('Model'), the model MyModel is shown in the list, so I assume it's loaded correctly. However, when I try to use the model (i.e. $this->MyModel->find() it never finds anything, it always returns an empty array.
Note that if I put the same exact model (MyModel) in the typical models folder (app/Model/) then it all works fine.
What am I missing to make this work?
EDIT
Ok, so it seems that the problem is in the connection to the database when the model is placed in that folder outside app. With the code shown above, Cake finds the model. However, when I do a find(), I get a missing table error for the datasource (default in this case).
Is it possible that the model isn't loading the correct database configuration because that configuration is inside the app/Config folder? How can I make that model load that configuration? If I have to put that configuration somewehre else (maybe in the same outside folder?) I can do that, but how do I tell the model to find it?
EDIT 2
I can see better what the problem is now. If I put a model in a different folder (other than app/Model) and use App::build() to set the path of that new folder, Cake finds it, there's no doubt (I use App::objects('Model') and the model is listed with all the other models from app/Model).
However, it's like Cake is not actually reading what's inside that model class, or at least not everything. It seems to read the $useDbConfig variable, but it ignores $useTable and any function I have defined in that model. Example of my model:
class Usuario extends AppModel {
var $name = 'Usuario';
var $primaryKey = 'id_usuario';
var $useDbConfig = 'BD_ControlAcceso';
function createTempPassword($len) {
//some code
}
}
If I do a $this->Usuario->find('all'), it returns all the records correctly. However, if I call $this->Usuario->createTempPassword(7) I get a Database Error.
I have another model (MyModel) in that same folder with a $useTable = 'mytable'. If I don a find() on it, I get an error saying that mytable table could not be found. However, if I do $this->MyModel->useTable = 'mytable' then it works fine.
How is this possible? What's going on here?
EDIT 3
I just want to add that I've done extensive testing and the issue is clear: Cake "knows" that the model is in the external folder (confirmed by printing App::objects('Model'), the model is listed there, and if I remove it from that folder then it's not listed). But even though it knows it's there, it ignores whatever is inside the model file. I've tried all the methods below to load the model but none of them worked. Is this a bug in CakePHP? If not, what am I doing wrong?
You should use App::uses('MyModel', 'Model') and is should go before the class declaration like so:
<?php
App::uses('MyModel', 'Model');
App::uses('AppController','Controller');
class UsersController extends AppController {
// controller class
}
Another thing to try is loading the model where you need it:
$this->loadModel('MyModel');
The other thing you can try is the Model instantiation in the top of your model class. Try updating your model to:
App::uses('AppModel','Model');
class Usuario extends AppModel {
var $name = 'Usuario';
var $primaryKey = 'id_usuario';
var $useDbConfig = 'BD_ControlAcceso';
function createTempPassword($len) {
//some code
}
}

Cakephp missing helper file error

I'm getting below error...I'm not sure what this means as I have included helper file in the view file...
Missing Helper File
Error: The helper file track/views/helpers/request_handler.php can not be found or does not exist.
Error: Create the class below in file: track/views/helpers/request_handler.php
<?php
class RequestHandlerHelper extends AppHelper {
}
?>
If you can let me know what this means that would be appreciated!
Thank you.
Jae
Unless you customized how your CakePHP works, this should apply to most cases:
Checklist
Make sure the helper file is created in /app/views/helpers/request_handler.php
Make sure the content of the request_handler.php looks like this:
class RequestHandlerHelper extends AppHelper {
var $name = 'RequestHandler';
//bla....
}
Make sure in the controller that renders the view has the helper array included
class FancyController extends AppController {
var $name = 'Fancy';
var $helpers = array('RequestHandler');
//bla....
}
I think that's all :)
Cheers
you have to include the helpers in the controller (app_controller if you want the helper to be available for views of all controller)
var $helpers = array('Form', 'Html', 'YourHelper');
If you are using any version of CakePHP, just open the file core.php in the folder config and edit the line Configure::write('debug', 0); to Configure::write('debug', 2);
It will say the erro and how to create the file and where to put it.

Controller not found in cakePHP

I have controller class EmployeeController in employee_controller.php file,and i have a model class Employee in employee.php ,database table is employees ,All the functions are working (such as findall() and read() are working fine),but i have add function which is like this
function add() {
if (!empty($this->data)) {
if ($this->Employee->save($this->data)) {
$this->Session->setFlash('Employee has been saved.');
$this->redirect(array('action' => 'index'));
}
When i tried to save ,An error EmployeesController not found will display and shows the following code
<?php
class EmployeesController extends AppController {
var $name = 'Employees';
}
?>
i am not able to solve this problem ,please help me out to get rid of this problem
The file should be called employees_controller.php, not employee_controller.php.
All controller files are named in the plural.
You can also learn about the naming of the tables and the conventions at http://cakeapp.com
I don't know if the question is still important, but the cakephp convention says that filenames for controllers must be in plural an without underscores, so your controller file should be named EmployeesController.php.

Resources