I want to do something like this in CodeIgniter:
Controller:
class Home extends CI_Controller {
public function index() {
$this->load->view('home');
}
public function user($user) {
return $user;
}
}
View (home):
Hi <?php echo user('User') ?> // call a controller method
How can i do this?
One way to get it is , returning it from the routes.php ,
Like $route['user'] = "home/user"; And this will execute the user method from the home controller.
Related
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
I want to create custom 404 page for my site.I mean when i type url as
http://example.com/anydummytext
need to redirect to custom 404page
How to create custom page for this
The simple way is create a controller you can name the error controller to any what you want.
Example
<?php
class Not_found extends CI_Controller {
public function index() {
// some content
$this->load->view('not_found');
}
}
Then on another controller you can redirect it
redirect('not_found');
Example Only
<?php
class Home extends CI_Controller {
public function index() {
$result = $this->some_model->get();
if ($result) {
// content
$this->load->view('home');
} else {
redirect('not_found');
}
}
}
The other option is in config/routes.php you can use codeigniter
$route['404_override'] = 'not_found'; // Note will not work in a subfolder
You can create a new controller or use existing controller to write down a function for loading the VIEW for 404.
class Nopage extends CI_Controller
{
/* Index function
*
* return void
*/
public function index()
{
$this->load->view('nopage_404');
}
}
}
Call the controller function in config->routes as below.
$route['404_override'] = 'nopage';
In my header view I wrote this code:
<?php
if($this->session->userdata('logged_in')) {
$query = $this->db->get_where('instructors', array('id' => $this->session->userdata('id')));
$insdatacheck = $query->row_array();
if($insdatacheck['name'] == '') {
redirect(base_url().'user/continueregistration');
} else { ?>
<script type="text/javascript">alert('test');</script>
<?php
}
}
?>
But it does not redirect to the following page. However, if I write this in the controller, it works properly. I wrote it in header view because I want to check it in every page where enters the user. How can I improve it and write in a proper way? Thanks in advance
I think instead of your header you should put your check inside your controller constructor.
class Test extends CI_Controller {
function __construct() {
parent::__construct();
// if not logged-in redirect to login page
if ($this->session->userdata('logged_in') == false) {
redirect('login'); // where you want to redirect
}
}
}
Another option is to create a base controller. Place the function in the base controller and then inherit from this.
To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application.
class MY_Controller extends Controller
{
public function __construct()
{
parent::__construct();
}
public function is_logged_in()
{
$user = $this->session->userdata('user_data');
return isset($user);
}
}
Then make your controller inherit from this base controller.
class X extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function do_something()
{
if ($this->is_logged_in())
{
// User is logged in. Do something.
}
}
}
First create session in the controller only,
Then we access session in any page,
$this->load->library('session');
$user=$this->session->userdata('logged_in');
if (!isset($user)) {
redirect(base_url().'user/continueregistration');
}
else {
<script type="text/javascript">alert('test');</script>
}
I have created two views in CodeIgniter and I have created controller named HelloWorld.php
It contains two views.. but my problem is that the second view never gets called.
http://localhost/CodeIgniter/HelloWorld/Hello
Works fine for me, but second view
http://localhost/CodeIgniter/HelloWorld/Buzz
Doesn't call the second view
Here is my code
<?php
class HelloWorld extends CI_Controller
{
var $name;
var $color;
function __construct()
{
parent:: __construct();
$this->name= 'Suzzu';
$this->color = 'aqua';
}
public function Hello()
{
$this->load->view("hello");
}
public function Buzz()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view("welcome",$data);
}
}
what's the problem ??
Your code works as expected for me provided that:
there is a view file "views/welcome.php"
there is a view file "views/hello.php"
Can you verify that HelloWorld::Buzz() is being called?
public function Buzz()
{
die('yes, it works');
}
If this function doesn't execute when you go to /localhost/CodeIgniter/HelloWorld/Buzz, can you provide the following:
$config['base_url'] (config/config.php)
$config['index_page']
config/routes.php contents
change $config['base_url']="localhost/codeigniter/contollername"
Layout
For all pages when I load the data works but the model and controller do not work. Other model which I have assigned works. I mean, one controller and model of layout for all pages but other page their own model and controller. One controller for all pages and one for each page.
<?php foreach ($sidebars as $row): ?>
<tr><td><?php echo $row->title; ?> </td></tr>
<?php endforeach; ?>
<div id="main" role="main" class="content">
{content}------here this show my pages content other model and controller
</div>
Controller
on my controller i load my page
now if i load other page again i have to enter the
$this->load->model('side_model');
$data['sidebars'] = $this->side_model->get_sidebar();
this one of my page controller it works but other controller if don't mention about so not works i want to make it as controller for every page and i don't want to mention in each page controller above code
"<?php"
public $layout = 'default'
public function index()
{
$this->load->model('side_model');
$data['sidebars'] = $this->side_model->get_sidebar();
$this->load->view('Layout/default',$data);
}
Model
function get_sidebar(){
$this->db->select('*');
$this->db->from('side');
$this->db->where('active','1');
$this->db->order_by('added_date', 'DESC');
return $this->db->get()->result();
How do I make one main controller and model with many function for all pages?
Create a file called core/MY_Controller.php:
class MY_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model("side_model");
$this->data["sidebars"] = $this->side_model->get_sidebar();
}
protected function get_html_meta($meta){
// method will be available for all extending controllers
return ucfirst($meta)." ".date("Y-m-d");
}
}
From now on you should extend MY_Controller and not CI_Controller, in your existing controller you can now pass the varible $this->data which is available to the view (i have added a few examples of $this->data usage and its scope:
class your_controller extends MY_Controller {
public function index(){
$this->data["html_meta"] = $this->get_html_meta("this is meta");
$this->data["html_title"] = "Hello World!";
$this->load->view('Layout/default',$this->data);
}
}
NOTE: unlike $this->data, the scope for $data is inside the function you declare it only.
As for a global model, once the MY_Controller constructor (or autoload.php) loads the model you can invoke the model as long as you other controllers extend MY_Controller.
Write you own controller
class ParentController extends CI_Controller {
public function index()
{
echo 'Hello World!';
}
}
class Blog extends ParentController {
public function index()
{
echo 'Hello World!';
}
}