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'));
}
Related
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');
}
}
I am new to coding and have fallen in love with codeigniter and recently moved from a wamp server to a LAMP server. I've extracted codeigniter into /var/www/ and set the root directory of the host in apache. I can see the codeigniter welcome page, however if I change the default controller in config.php to my controller and load this controller:
class Site extends CI_Controller {
public function index() {
$this->load->view('home_view');
}
}
I get this text in my browser:
$this -> load -> view ('home_view'); }}
The only line of my index function as well as a codeigniter generated 404 error below it.
base_url() and default controller are set, codeigniter welcome page works fine, but my page does not.
Does anyone have an idea as to why? Your help is very much appreciated.
Make sure your controller is inside application/controllers and the name is site.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home_view');
}
}
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;
I have a question which regards to controllers. Let's get it started:
I have a main controller named "admin.php", it has a menu for company, user management, etc. Each item on the menu has separate PHP file to hold different kinds data [I seems to be lengthy to combine them all in one php.
So for this example:
I have 3 controllers: admin.php , company.php, usermanagement.php
What I want is, link the company and management controllers as a child of admin. So if enter the address on the browser, it may look: localhost/admin/company and localhost/admin/usermanagement
I configured the routes and it's good but when I enter "localhost/company" it loads the company page which i didn't want to. i wanted to link them all as a child of an admin page.
How would I achieve this?
by the way here's a snippet of my code:
admin.php - Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_admin');
}
}
Company - Same as the admin
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller {
function __construct(){
parent::__construct();
session_start();
}
public function index() {
$this->load->view('view_company');
}
}
Thanks,
James
EDIT: I have tried adding functions on the admin.php like:
function company() {}
function usermanagement() {}
but I guess it wasn't that effective since it will include lots of functions later on as I try to migrate my native php codes into this MVC architecture framework.
If your issue is that you like the way routes work but don't want people to be able to visit index.php/company/ and rather prefer that they visit admin/company you can always do:
class Company extends CI_Controller {
public function __construct() {
parent::__construct();
if ( $this->uri->segment(1) != "admin" ) {
redirect('admin/company/'.$this->uri->segment(3));
}
}
...
Although bear in mind that you would probably need a more complete URL forming method than just adding $this->uri->segment(3) but the general pattern is there.
You can add more functions in your 'admin' controller, so, default page is:
public function index() {
$this->load->view('view_admin');
}
'Subpage' is:
public function company() {
$this->load->view('view_company');
}
etc, etc...
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);
}
}