I am new to CodeIgniter MVC. My home view is loaded correctly. When i replace home in index() with aboutus it is working, but when i call aboutus function then it is showing 404 file not found error. Login is also working correctly. I am not getting what is wrong. My view folder contains aboutus, home and login files.
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
//Here the html code
<li>Login</li>
<li><a href="<?php echo base_url();?>index.php/aboutus" >About Us</a></li>
How to load default controller
In config/routes.php
$route['default_controller'] = "controller_name; //this load index() in provided controller
$route['default_controller'] = "controller_name/method_name"; //this load method which you created inside provided controller(ex: main/about_us)
How to create controller
Path - application/controllers/
File name - main.php
inside main.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->load->view("home");
}
function login()
{
$this->load->view("login");
}
function aboutus()
{
$this->load->view("aboutus");
}
}
How to create view
Path - `application/view/`
File name - `home.php`
File name - `login.php`
File name - `aboutus.php`
How to use Link
Home
Login
aboutus
How to use base_url()
in config/autoload.php
$autoload['helper'] = array('url');
in config/config.php
$config['base_url'] = '';
Change your html like this
home
about us
You need to add route to your new function aboutus() in routes.php
You can find routes.php in "application/config/".
suppose your controller name is 'Client_home'
Then,
eg : $route['default_controller'] = 'Client_home';
This is the reason why aboutus is working when you replace home in index() with aboutus . The index() method will be called by default, if you are not specifying which function you want to initiate.
For the aboutus() method / function, the route will be like this :
eg : $route['aboutus'] = "Client_home/aboutus";
Synatx:
$route['url'] = "controllername/method or function name"
Example for this question. Hope this help you:
http://localhost/ciexample/index.php/your_controller/your_function
i would like to know the controller name and also would like to know where u have change its configuration , if yes than which config .
use this in address bar
"localhost/folder_name_of_the_project/index/controller_name"
Related
I create a Home Page and when I want to access it's written Page Not Founded
I create in my rount.php access but again there is same error
$route['pages/index']= 'pages/home';
Do I need to change my controller or model or just route.php file
Pages controller
<?php
class Pages extends CI_Controller{
public function index(){
$this->load->view('pages/home');
}
public function view($page='home'){
if(!file_exists(APPPATH. 'views/pages/'.$page.'.php')){
show_404();
}
$data['title']= ucfirst($page);
$this->load->view('templates/header');
$this->load->view('pages/'.$page,$data);
$this->load->view('templates/footer');
}
}
try like this . and make sure you are linking right base_url for home index function like this. follow this if its help .
echo base_url("pages/home");
$route['pages/home']= 'pages/index';
Default Controller is HomePage.php
$route['default_controller'] = 'index/home_page';
Index.php
public function home_page()
{
$data['subview']='subview/home_page';
$this->load->view('_layout_home.php',$data);
}
_layout_home.php
<?php $this->load->view('include/header_home')?>
<?php $this->load->view($subview)?>
<?php $this->load->view('include/footer')?>
There is no autofocus attribute used in HTML or CSS.
The above code is for a home page of CodeIgniter web page which loads from bottom to top.
First letter only must be upper case on class and filename
https://www.codeigniter.com/user_guide/general/styleguide.html#file-naming
Change filename from
HomePage.php // Wrong Way
To
Homepage.php // Correct Way
Controller
https://www.codeigniter.com/user_guide/general/styleguide.html#class-and-method-naming
https://www.codeigniter.com/user_guide/general/views.html#loading-multiple-views
Path application > controllers > Homepage.php
https://www.codeigniter.com/user_guide/general/controllers.html
<?php
class Homepage extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
}
public function home_page() {
$data['subview']='subview/home_page';
// No need for .php
$this->load->view('layout_home',$data);
}
}
Then on routes.php
$route['default_controller'] = 'homepage/home_page';
https://www.codeigniter.com/user_guide/general/routing.html#examples
View
filename layout_home.php
<?php $this->load->view('include/header_home')?>
<?php $this->load->view($subview)?>
<?php $this->load->view('include/footer')?>
I am new to codeIgnitor and keep getting the error 404, please advise where am I going wrong, below is my structure and pages.
database name: stoma_store_suppliers ,
Database fields: supplierid, supplier_name, supplier_phone
Models Page:_____________suppliers.php
<?php
Class Suppliers extends CI_Model
{
public function get_suppliers($supplierid) {
if($supplierid != FALSE) {
$query = $this->db->get_where('store_suppliers', array('supplierid' =>$supplierid));
return $query->row_array();
}
else {
return FALSE;
}
}
}
?>
Controllers Page__________
suppliers.php
<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
class Suppliers extends CI_Controller {
public function show($supplierid) {
$this->load->model('suppliers');
$store_suppliers = $this->suppliers->get_suppliers($supplierid);
$data['supplier_name'] = $suppliers['supplier_name'];
$data['supplier_phone'] = $suppliers['supplier_phone'];
$this->load->view('index', $data);
}
}
?>
Views Page:
index.php
<?php print $supplier_name; ?>
<?php print $supplier_phone; ?>
Required things to access any page of codeigniter are:
First letter of controller file must be capital like Suppliers.php.
The first letter of the class definition must match the filename, e.g. for Suppliers.php it would be class Suppliers extends CI_Controller.
If a database is to be used, then the correct connection details must be set in the file config/database.php.
Below are the steps to access the any page created in the codeigniter.
Write your site domain followed by index.php, e.g. http://mysitedomain.com/index.php/suppliers
To remove the need for index.php in the URL add an .htaccess file at the root folder. Details here. In that case the URL will be
http://mysitedomain.com/. Now, domain name will get followed by controller name and the function to be accessed, e.g.
http://mysitedomain.com/suppliers/show
To pass arguments to a controller using the URL add them after the controller/function segments, e.g. http://mysitedomain.com/user/show/dede
If a controller has an index function - public function index(){ - it can be accessed two ways:
A) http://mysitedomain.com/suppliers/index
B) http://mysitedomain.com/suppliers
If /function/argument URI segments are not provided after controller name, codeigniter will call index() by default.
So I have this setup:
ARTISTAS
But when I click on that link, it takes me to http://localhost/Universal%20Music/Universal/artistas/index which says 404, like if there's nothing there but I've already created the controller with that name loading some views in it. Am I missing something here?
EDIT
My config.php has:
$config['base_url'] = 'http://localhost/Universal/Universal';
I stop using spaces in the names of the folders and now the path is:
C:\Users\Horacio\Documents\Projects\Universal\Universal
The controller file has the same name of the class I need and the code inside is:
<?php
class Artistas extends CI_Controller {
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('header');
$this->load->view('main-index');
}
}
I like to pass a variable to a controller trough the URL like this:
a href="<?php print base_url('/profile/' . $this->session->userdata('id')); ?>">Profiel</a>
/profile/ = controller
$this->session->userdata('id') = variable
Now I want to give it to Profile but it only works when I do /profile/index/1 and I like to it like this: /profile/1
When actually to use a new controller?
This is my controller:
<?php
class Profile extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('login');
$this->load->helper('url');
}
function index()
{
$this->load->view('profile_view');
}
}
?>
Found it,
I just need to add this line to the routes.php file in the config folder:
$route['profile/(:num)'] = "profile/index/$1"
Then when profile/1 is entered it will automatically think it's profile/index/1
Or you could use _remap function in your controller class. Read the related part in users guide. It is under "controllers" part.