CodeIgniter - Hyperlink creation - php

I have tried to create a hyperlink to another page using anchors however it doesn't seem to work.
My function is called calendar and its stored in calcontrol.php within a controllers directory.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}
?>
I have tried other methods but they do not seem to work.
edit : this is my a href
Link

Filename and classname must be the same, otherwise CI will not be able to load it. And if using CI 3, make sure your class name and filename are both Ucfirst. If you have everything set up like this, then you should be able to view the page through http://yourdomain.com/calendar/calendar

Try this code. You forgot to load url helper. File Calendar.php - nogice capital C if CI3:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Calendar extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('calendar');
}
}

Related

In Codeigniter extends one controller into another controller

I am using codeigniter(3.1.5) and Ihave two controllers in my application/controllers/ folder. with name controller A and Controller B. I want to extends Controller A in Controller B so that I can use methods of Controller A. But it generates class not found error.
Here is my sample code:
A_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class A_Controller extends CI_Controller {
public function index()
{
}
public function display(){
echo 'base controller function called.';
}
}
B_Controller.php:
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
}
I want to execute display() method of controller A in controller B.
If i put controller A in application/core/ folder and in application/config/config.php file make
$config['subclass_prefix'] = 'A_';
then I can able to access methods of controller A.
Please suggest.
Thanks in advance.
Extending a controller in another controller is not really good. Building with MVC and especially with CI, you have other options to achieve this.
Use a class MY_Controller inside application/core that extends the CI_Controller. Later, all (or some) of your controllers should extend the MY_Controller. In MY_Controller you can have many functions and you can call which function you want in your controller.
Use a library. Write your own library in application/libraries and load it in your controller wherever you want. A library is a class with functionality for your project.
Use a helper. Write your own helper in application/helpers and load it in your controller. A helper should have a general purpose for your application.
In that way, your code will be more flexible and reusable for the future. Messing with 2 Controllers seems bad to me. Remember that with the default Routing system of CI you can be confused.
Try to use following code.
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function test()
{
$this->display();
}
}
Use construct in controller
defined('BASEPATH') OR exit('No direct script access allowed');
class B_Controller extends A_Controller {
public function __construct() {
parent::__construct();
}
public function test()
{
$this->display();
}
}
I found the solution using including parent controller on child controller like this -
require_once(APPPATH."modules/frontend/controllers/Frontend.php");
then my function like this -
class Home extends Frontend {
function __construct() {
parent::__construct();
}
function index() {
echo $this->test(); //from Frontend controller
}
}
I hope this will help.
add this script in B_Controller :
include_once (dirname(__FILE__) . "/A_Controller.php");
for example B_Controller.php :
defined('BASEPATH') OR exit('No direct script access allowed');
include_once (dirname(__FILE__) . "/A_Controller.php");
class B_Controller extends A_Controller {
}

Code Igniter not running the method in the controller

I have a form in a view called login_view that I would like to target a function in the Login controller.
For my form in login_view I have echo form_open('/Login/validate_credentials');
In my Login.php controller I have:
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller{
function __construct()
{
parent::__construct();
}
public function validate_credentials(){
$this->load->view('welcome2');
}
}
?>
When I submit the form from the browser, instead of loading the welcome2 view like I expect, it tries to load the URL of:
localhost/project/Login/validate_credentials
and I just see the default page for WAMP.
It's my first day in Code Igniter so I'm sure the answer is simple.
Just replace your controller construct by this:
public function __construct()
{
parent::__construct();
$this->load->helper(array('url','text','html','form','string'));
}

Method load fails in codeigniter 3

I was using codeigniter 2.2.1. And now codeigniter 3 released. I just tried it and ended up with error.
When I try to load method as in codeigniter2.x, it shows
Unable to locate the model you have specified: Demo
where Demo is my method file.
Controller - welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('demo');
}
public function index() {
$data = $this -> demo ->check();
print_r($data);
}
}
Model - demo.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Demo extends CI_Model {
public function __construct() {
$this->load->database();
}
}
I can't figure-out what is wrong with this code. Please help. Thank you in advance..
Edit:- This is works well in my wamp machine. But I am checking it now in another local machine where my institute host the websites. There it is not working
It was a small issue. I changed model name from demo.php to Demo.php. And it works...

Codeigniter + ION AUTH...Can't tell if logged in

I'm building a Codeigniter application with ION Auth, which is going relatively OK. I've set up the ION Auth framework following this guide http://www.rappasoft.com/tutorials/5-building-a-simple-codeigniter-application-using-ion-auth.html#.U0Q5ufldUrU and when I log in using the default username and password I'm redirect to my homepage. So far, so good.
Problem then is that when I access one of my protected pages (i.e. a controller which extends MY_Controller), I'm redirected to the login page again.
I'm quite new to this, so just looking for some pointers of where to look. It seems I'm being logged in, but I don't really know how to check for sure.
I've tried this line of code in my view, but I get a couple of errors: user_info is undefined & trying to get property of a non object.
<?php echo $user_info->username;?>
MY_Controller in application/core:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if (!$this->ion_auth->logged_in()) {
redirect('auth/login');
} else {
//Store user in $data
$data->user_info = $this->ion_auth->user()->row();
//Load $the_user in all views
$this->load->vars($data);
}
}
}
My 'members only' area controller class declaration:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Manage extends MY_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library('grocery_CRUD');
}
//other functions omitted
Add this to your MY_Controller and see what is returned
var_dump($this->ion_auth->logged_in());exit;

Add public variable to all CI_Controller's and anything that extends them?

Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.
The options I've thought of;
Edit CI_Controller and add it there... guessing that's a bad idea.
Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.
Any other clever ways?
I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?
Thanks!
[edit] Using PHP 5.3.x [/edit]
http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
You can extend CI_Controller to have the functionality you want.
application/core/MY_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function my_function()
{
return "Cool return from my_function";
}
}
controllers/welcome.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index()
{
echo $this->my_function();//echo's "Cool return from my_function"
}
}
You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.
I think what you want can be easily achieved using traits. Check here: PHP: Traits
More specifically -> Example #11 Defining Properties.
The only limitation is it's PHP 5.4+
You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
}
protected function loadPage($path, $data = array())
{
$this->load->view('common/head'); // Assuming you will use a folder for page parts
$this->load->view($path, $data);
$this->load->view('common/foot');
}
}
Afterwards in your page controller you can call upon the same methods much more easily:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class User extends MY_Controller {
public function index()
{
$data = array(); // get data from model
$this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
}
public function edit()
{
$data = array(); // get data from model
$this->loadPage('user/edit', $data);
}
}

Categories