Timezone helper in Cakephp - php

I'm trying to populate the select box with countries' timezones. I have seen examples and answers here, but nothing works out for me. I am working on Cakephp 2.3.
My timeZone helper class is in this directory
App/View/Helper/TimeZoneHelper.php.
Here is my controller:
class TimeZoneController extends AppController{
public function index(){
$helpers = array('TimeZoneHelper');
}
}
my view
<?php
echo $timezone->select('timezone');
?>
It isn't working, and I don't know how it works because I have never used this functionality before.

You are working with cake2.x. But you are using 1.x syntax.
The correct syntax for 2.x is:
echo $this->Timezone->select('timezone');
(instead of $timezone-select)
Its also in the documentation by the way:
http://book.cakephp.org/2.0/en/views/helpers.html#using-helpers

When you want to add helper in action you need to use following code
class TimeZoneController extends AppController{
public function index(){
$this->helpers[] = 'TimeZoneHelper';
}
}
If you want the helper to available to all the action
class TimeZoneController extends AppController{
public $helpers = array('TimeZoneHelper');
public function index(){
}
}
If you want the helper to be available to all controllers then you need to add it to /app/Controller/AppController.php
class AppController extends Controller{
public $helpers = array('TimeZoneHelper');
}

Related

Call Model from custom component into Joomla 4 controller

I have been working in Joomla 4.2.5 and I need to convert my custom components from Joomla 3 to Joomla 4 compatible. I am trying to call model from my custom component into Joomla user controller file, but I am getting an error and code does not perform execution.
I have tried few code but it is not working.
Joomla 3 code for call model from custom component into user controller:
UserController.php
JModelLegacy::addIncludePath(JPATH_SITE . '/components/com_mycomponent/models', 'MycomponentModel');
$model = JModelLegacy::getInstance('Mycomponent', 'MycomponentModel');
Here is my model file code in
components/Mycomponent/Models/mymodel.php
class MycomponentModelmycomponent extends JModelLegacy
{
public function somefunction(){
}
}
Below is my attempt to convert above code into Joomla 4:
UserController.php
Use Joomla\CMS\MVC\Model\BaseDatabaseModel;
class UserController extends BaseController
{
public function login()
{
login functions....
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
$model->myfunction($argument);
}
}
components/Mycomponent/Models/mymodel.php
namespace Joomla\Component\mycomponent\Site\Model;
use Joomla\Registry\Registry;
use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\ListModel;
class IbousersModelIbousers extends ListModel
{
public function myfunction($argument){
}
}
But it is not working, can anyone suggest me what is wrong or how can I call model from another component in Joomla 4?
In Joomla4, you get the model from the MVCFactory.
So the equivalent of:
BaseDatabaseModel::addIncludePath(JPATH_SITE .'/components/com_mycomponent/models');
$model = BaseDatabaseModel::getInstance('Mycomponent', 'MycomponentModel');
Would be:
$mvc = Factory::getApplication()->bootComponent('com_mycomponent')->getMVCFactory();
$model = $mvc->createModel('MycomponentModel', 'Site');

Add another controller in codeigniter

Hello I'm currently facing a problem of adding another controller and the problem is that
I have 2 controllers
class 1st_Controller extends CI_Controller {
}
and
class 2nd_Controller extends CI_Controller{
my 2 models are working perfectly fine and the only problem is that I need to call 1 model per each controller
for example
1st controller is for 1st model and 2nd controller is for 2nd model.
Now what I've tried so far is
class 2nd_Controller extends 1st_Controller {
public function __construct()
{
header("Access-Control-Allow-Origin: *");
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
$this->load->helper('url');
$this->load->library("pagination");
$this->load->library("session");
}
public function index()
{
$data['title'] = 'System Login';
$get_all_inv = $this->2ndmodel->get_all();
$data["tryvariable"] = $get_all_inv;
$this->template->load('default_layout','contents','myview2nd', $data);
}
}
I tried echo in my view like this
<?php echo $tryvariable; ?>
but no luck because the error says that it is an undefined variable .
Your second controller can't be define cause it's not define as subclass_prefix in your codeigniter application.
class 2nd_Controller extends 1st_Controller { //codeigniter don't recognize this.
}
The simpliest way to solve your problem is.. the fact that you can call multiple models in 1 Controller.
so you can have:
class 1st_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('1st_model','1stmodel');
$this->load->model('2nd_model','2ndmodel');
}
}
or call just once.
class 2nd_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('2nd_model','2ndmodel');
}
}
Hope that helps.
So what I did was write like this on my route to have my 2nd_Controller working
$route['default_controller'] = '1st_Controller';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['2nd_Controller'] = '2nd_Controller';
Now everything is working perfectly. Thanks for all the help

Access an array in all views in codeigniter

I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.

How to load methods of a controller from another one?

I have the following controller:
class Tests extends CI_Controller {
public function update_record_test()
{
//some methods
}
}
?>
But I need to execute Method1 method from Controller1 controller. How can I do it?
There's not much information here, or I may be missing something, but what's wrong with
class Tests extends CI_Controller {
public function update_record_test()
{
$controller1 = new Controller1();
$controller1->Method1();
}
}
If you mean you want to execute a function of your main class CI_CONTROLLER, try this.
parent::Method1();
Check the http://php.net/manual/en/keyword.extends.php for more examples

Same data variable passing to view

In my project, I have one search section with 3 select box. I passed the value to it using
$data['restaurant_all']=$this->restaurant_model->get_all('','','','','','yes')->result();
$data['state_all']=$this->state_model->get_all();
$data['menu_all']=$this->menu_model->get_all('all','','','','','','yes')->result();
$data['restaurant']=$this->input->post('restaurant');
$data['state']=$this->input->post('area');
$data['food_type']=$this->input->post('menu');
I need this statement in all my pages. In there any way to accomplish this without writing these statements in all the pages
a. extend the default controller by creating a file MY_Contoller.php at a suitable location.
b. create a custom class that will extend the default controller.
c. add a protected or public variable $data to custom class.
e. do something with data using __construct()
d. make every controller extend the custom controller.
e. you can access this variable like any other class variable.
example code:
MY_Controller.php
class APP extends CI_controller {
protected $data;
function __construct() {
parent::__construct();
$this->_init();
}
function _init() {
$this->data['state'] = $this->input->post('area');
}
}
normal controllers:
class Welcome extends APP {
function __construct() {
parent::__construct();
}
function view() {
/* pass this data value like normal data param */
$this->load->view('some_view', $this->data);
}
}
hope it helps.
Use constants, in /config/constants.php

Categories