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');
}
}
Related
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'));
}
We did a fresh install of codeigniter 3.0.6 in our hosting and everytime we are changing
$route['default_controller'] = 'welcome';
to
$route['default_controller'] = 'home';
we are receiving a 404 error message.
Does anyone know how to fix this?
CodeIgniter 3.0+ has changed there naming conversations.
In your image your controller name define as home.php. Check first letter. Its should be caps.
change home.php to Home.php
Inside Home.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
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...
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...
I am utterly and completely confused. I've looked through all the documentation and I can't find a solution. Setup: CI 2.1.3, QNAP server.
Problem: I cannot use a private function on a controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Signup extends CI_Controller {
public function index()
{
_loadcontent();
}
private function _loadcontent()
{
$this->load->view('welcome_message');
}
}
/* End of file signup.php */
/* Location: ./application/controllers/account/signup.php */
it won't work and gives an HTTP Error 500 (Internal Server Error). HOWEVER, placing the file in the main controller directory works. Does anybody have any clue? is this a bug? Thanks in advance.
You need to call the function using $this.
public function index()
{
$this->_loadcontent();
}