I want to know how can I write a component in codeigniter
I have worked with symfony1.4 and there there is something include_component("name",dataarray()) that we can load a component ( it's actually an action ).
I already know that we have $this->load->view('admin/template/login') for loading a view, But I want to know is there any way to call an action like this?
this->load(news/list,array('date'=>xxx-xx-xx))
thanks anyways
you can this through the use of libraries, helpers or plugins.
an example of using a library class in application/library called LoadNews.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LoadNews {
public function showList($date_array) {
// do stuff with date array
}
}
/* End of file LoadNews.php */
/* Location: ./application/libraries/LoadNews.php */
then in your controller, you call
$this->load->library('LoadNews');
$this->loadnews->showList(array('date'=>'2014-12-19'));
Yes, you can do this from CI 3.
In your Controller or View just call $this->load->view(view, data);
Example:
<?php
defined('BASEPATH') or die('No direct script allowed');
class PageController extends CI_Controller
{
public function index()
{
$data = [
'names' => ['john', 'doe'],
'ages' => ['24', '30']
];
$this->load->view('index', $data);
}
}
?>
And in your view you can access 'names' and 'ages' as $names and $ages respectively.
You can do this in your view:
<?php
echo '<pre>';
print_r($names);
print_r($ages);
echo '</pre>';
?>
The result:
Array (
[0] => john
[1] => doe
)
Array (
[0] => 24
[1] => 30
)
Related
My controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Calendar extends MX_Controller
{
function index()
{
$this -> display( $year=null, $month=null);
}
function display($year=null,$month=null)
{
if( ! $this->bitauth->logged_in())
{
$this->session->set_userdata('redir', current_url());
redirect('access/login');
}
$data['header']=Modules::run('header/header/index');
$data['footer']=Modules::run('footer/footer/index');
$conf=array(
'start_day'=>'monday',
'day_type'=> 'long',
'show_next_prev'=>true,
'next_prev_url'=>'http://datacentral.demo/calendar/'
);
$this->load->library('calendar',$conf);
$this->load->view('calendar',$data);
}
}
My views
<?php echo $this->calendar->generate($this->uri->segment(3), $this->uri->segment(4)); ?>
When I change the month, the url looks normal eg. http://example.com/calendar/2017/06 but I keep getting this 404 error not found page.
No route specified in my config file. What am I missing?
I believe you will need to create a route liek below:
$route['calendar/(:any)/(:any)'] = 'calendar/display';
Try this route link:
$route['calendar/(:any)/(:any)'] = 'calendar/display/$1/$2';
I have a form with two fields
<input type="text" name="total_plots" value="" placeholder="Enter Total plots" />
<input type="text" name="available_plots" value="" placeholder="Enter Available Plots " />
Available plot "available_plots" field value should be less than total plots "total_plots" field value
I don't want to write callbacks. I want to extend the form validation rule.
How to ?
MY_Form_validation
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
public function __construct()
{
parent::__construct();
$this->CI =& get_instance();
}
public function check_avail($str)
{
$this->CI->form_validation->set_message('check_avail', 'Available plot should be less than Total plot.');
$total_plots = $this->CI->input->post('total_plots');
//echo '------'.$total_plots;
//echo '------'.$str;
if($str > $total_plots){
return false;
}
}
} // class
I have written rules in config
<?php
$config['plot_settings'] = array(
array(
'field' => 'total_plots',
'label' => 'Total Plots',
'rules' => 'trim|xss_clean'
),
array(
'field' => 'available_plots',
'label' => 'Available Plots',
'rules' => 'trim|xss_clean|check_avail'
)
);
?>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Plot extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('Admin_layout');
$this->load->model('admin/plot_model');
$this->config->load('plot_rules');
$this->output->enable_profiler(TRUE);
$this->new_name='';
}
public function add(){
$this->form_validation->set_rules($this->config->item('plot_settings'));
$this->form_validation->set_error_delimiters('<p><b>', '</b></p>');
if ($this->form_validation->run('submit') == FALSE )
{
$this->admin_layout->set_title('Post Plot');
$this->admin_layout->view('admin/post_plot');
}
}//add
}
I think you can do this without writing a callback or extending the validation rule.
CI already provides a validation rule to check for less_than value.
$total_plots = $this->input->post('total_plots');
$this->form_validation->set_rules('available_plots', 'Available Plots', "less_than[$total_plots]");
It should work.
Hi i have this data passed in the url
http://www.test.com/dashboard/john-doe
i want to get the page when i enter http://www.test.com/dashboard/john-doe
john doe is a name from the database i retrieve when the users can login.
so dashboard is a controller then how will i able to get the page if i passed a data john-doe next to dashboard controller? can someone help me figured this thing out? Any help is muchly appreicated this is my controller below
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('users_model', 'um');
$sessionData = $this->session->userdata('loggedIn');
$this->data['id'] = $sessionData['userID'];
if(!$this->session->userdata('loggedIn')){
redirect('login');
}
}
public function index($slug){
echo $slug; exit;
$this->data['title'] = 'Dashboard | test';
$this->data['menu'] = 'dashboard';
//get CustomerInfo
$this->data['getCustomerInfo'] = $this->um->getCustomerInfo($this->data['id']);
$this->template_lib->set_view('templateLogIn', 'dashboard', $this->data,'',$this->data);
}
}
The index function is loaded by default if the second segment of the URI is empty. If you have parameters you need to define a route:
$route['dashboard/(:any)'] = "dashboard/index/$1";
I have using a lot config form_validation file. It's working good!
But now I'm trying to get it work with controller in subfolder
/controllers/panel/users.php
My form_validation config file looks like
$config = array(
'panel/users/edit/' => array(
array('field' => 'login', 'label' => 'Логин', 'rules' => "trim|required|valid_email")
)
And my Users controller is
public function edit($user_id = FALSE)
{
if ($this->input->post('save'))
{
$this->load->library('form_validation');
if ($this->form_validation->run())
{
// Do some
}
}
}
But $this->form_validation->run() is always return FALSE
It isn't designed to work this way, there was a relevant change to ruri_string() #122 which would have fixed this but it had other repercussions and needs to be rethought.
You can call your validation rule group explicitly (drop the trailing slash from your rule group name)
if ($this->form_validation->run('panel/users/edit'))
or, if appropriate in your situation, workaround this by prepending uri->segment(1) to the auto-detected rule group.
application/libraries/MY_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
function run($group = '')
{
// Prepend URI to match subfolder controller validation rules
$uri = ($group == '') ? $this->CI->uri->segment(1) . $this->CI->uri->ruri_string() : $group;
return parent::run($uri);
}
}
I am really new to Codeigniter, and just learning from scratch. In the CI docs it says:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Someclass {
public function __construct($params)
{
// Do something with $params
}
}
Can you give me simple example how to pass data from controller to external library using array as parameters? I'd like to see a simple example.
All Codeigniter "library" constructors expect a single argument: an array of parameters, which are usually passed while loading the class with CI's loader, as in your example:
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass', $params);
I'm guessing you're confused about the "Do something with $params" part. It's not necessary to pass in any params, but if you do you might use them like this:
class Someclass {
public $color = 'blue'; //default color
public $size = 'small'; //default size
public function __construct($params)
{
foreach ($params as $property => $value)
{
$this->$property = $value;
}
// Size is now "large", color is "red"
}
}
You can always re-initialize later like so, if you need to:
$this->load->library('Someclass');
$this->Someclass->__construct($params);
Another thing to note is that if you have a config file that matches the name of your class, that configuration will be loaded automatically. So for example, if you have the file application/config/someclass.php:
$config['size'] = 'medium';
$config['color'] = 'green';
// etc.
This config will be automatically passed to the class constructor of "someclass" when it is loaded.
In libraries directory create one file Someclass_lib.php
Here is your Library code
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Someclass_lib
{
public $type = '';
public $color = '';
function Someclass_lib($params)
{
$this->CI =& get_instance();
$this->type = $params['type'];
$this->color = $params['color'];
}
}
Use this code when you want to load library
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('Someclass_lib', $params);