I am new to codeigniter. I have just made a login feature in CI with the help of google, but here I am redirecting to URL on login but It is not working.
Here is the details
after redirecting the link is like this http://localhost/xpensepedia/index.php/home
which is giving the 404 error
404 Page Not Found
The page you requested was not found.
and my controller is
public function index() {
$this->load->view('header');
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view("register");
}
$this->load->view('footer');
}
My files are in the image
Kindly check name of class you given to home.php Controller
Try this...
public function index() {
if (($this->session->userdata('user_id') != "")) {
redirect(site_url('home'));
} else {
$this->load->view('header');
$this->load->view("register");
$this->load->view('footer');
}
}
Home Controller :
class Home extends CI_Controller {
Public function index {
$this->load->view('header');
$this->load->view("home");
$this->load->view('footer');
}
}
you can add url path on constraints ....
you define the url like this on constants :
define('ROUTE_STIE_PATH','localhost/xxx');
define('ROUTE_STIE_PATH_LOGIN',ROUTE_STIE_PATH.'login/');
define('ROUTE_STIE_PATH_HOME',ROUTE_STIE_PATH.'home/');
and then you can echo by simply
echo ROUTE_STIE_PATH_LOGIN
if you want to call any methods with in home then
echo ROUTE_STIE_PATH_HOME.'methodname';
I solve similar case when I change my config file located in application>config>config.php to the following value:
$config['base_url'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
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';
Here i'm creating a login system. Once the user is verified the controller redirects the page to home.php.
There are 2 pages here login_registration.php and home.php
login_registration.php consists login and registration form from here the user is redirected to home.php. both the files are in path C:\wamp\www\CI-Project_1\application\views\login
The issue is it show 404 Page Not Found when redirected.
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
//LOAD MODELS HERE
$this->load->model('Login_model');
}
public function index(){
$this->load->view('login/login_registration.php');
}
function login_user(){
$login_btn = $this->input->post('login_btn');
if($login_btn == TRUE){
$arr['login'] = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$query = $this->Login_model->users_login($arr);
if($query->num_rows() > 0 && $query->num_rows() == 1){
$this->load->library('session');
$this->session->set_userdata($arr['login']);
redirect('login/home.php', 'location');
}
else
{
$this->index();
}
}
else
{
$this->index();
}
}
}
THANKS IN ADVANCE
According to codeigniter views documentation
The .php file extension does not need to be specified unless you use something other than .php.
So, the correct code is as follow:
$this->load->view('login/login_registration');
Also you are not using the redirect function correctly, as i don't see any home function inside login controller. If you somehow have home function inside login controller then why are you using .php after function name?
Check the codeigniter redirect documentation for more info.
You have to mention the name of your controller and method name where you want to redirect something like this
redirect('controllername/methodname');
Try this,
class Login extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->helper('url');
//LOAD MODELS HERE
$this->load->model('Login_model');
}
public function index(){
$this->load->view('login/login_registration.php');
}
public function home(){
$this->load->view('login/home.php');
}
function login_user(){
$login_btn = $this->input->post('login_btn');
if($login_btn == TRUE){
$arr['login'] = array(
'username'=>$this->input->post('username'),
'password'=>$this->input->post('password')
);
$query = $this->Login_model->users_login($arr);
if($query->num_rows() > 0 && $query->num_rows() == 1){
$this->load->library('session');
$this->session->set_userdata($arr['login']);
redirect('login/home', 'location');
}
else
{
$this->index();
}
}
else
{
$this->index();
}
}
}
Why you going to redirect if you want to call any method from a controller then you can redirect like that:
public function home(){
$this->load->view('login/home.php');
}
redirect('login/home', 'location');
But I think you don't need to redirect you can simply load view file just like you already load your registration view. So you just load view Instead of redirect..
$this->load->view('login/home.php');
To redirect on any function in CI can use redirect('controller n_name/function_name','refresh');
For redirect and $this->load->view, don't use .php.
Use something like this:
redirect('login/home', 'location');
I want to pass parameters to index function and if the parameter is not, redirect to home page. i added custom route also. where did i miss the code?
$route['main/(:any)'] = 'main/index/'; // looks like has a mistake
www.example.com ->when deafult index function
www.example.com/females ->when deafult index has string parameter 'female'
class Main extends CI_Controller {
public function index($gender) {
load->view('inc/header_view');
if((isset($gender)) && ($gender =='female')){
$this->load->view('female_view');
} else {
$this->load->view('female_view');
}
}
You can achieve this by following code
class Main extends CI_Controller {
public function index($gender="") {
if($gender == "")
{
$this->load->view('home');
} else {
$this->load->view('female_view');
}
}
Also as you mentioned to get this URL to work www.example.com & www.example.com/females , you need to add following into your routes.php
$route['default_controller'] = "main";
$route['(:any)'] = 'main/index/$1';
Let me know in case of any queries
You should try it like
$route['main'] = 'main/index';
$route['main/(:any)'] = 'main/index/$1';
Or you can use uri segments.
http://www.codeigniter.com/user_guide/libraries/uri.html?highlight=uri%20segments#CI_URI::segment
In codeIgniter I have created routes to remove the controller/methodname from the url so but the problem is that when I try to access the second controller index method it is not loading it's loading the home controller
I have 2 controller Home.php and Admin.php when i type in localhost/foldername/ it is opeing home conrtollers index method but when I type in localhost/foldername/admin it is redirecting to home controller's second method which is page can any one help me out in writing the routes.
Here is my routes which I have created
$route['default_controller'] = 'home';
$route['([^/]+)/?'] = 'home/page/$1'; //If I comment this it is working properly for me
Admin Controller Admin.php
class admin extends CI_Controller {
public function index() {
if($this->session->userdata('is_logged_in') == true) {
$this->load->view('admin/dashboard');
} else {
$this->load->view('admin/login');
}
}
}
Home Controller Home.php
class home extends CI_Controller {
public function index() {
$front_page = $this->get_data->front_page();
$page_data = $this->get_data->AllData('pages', $front_page);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
$page_content = $page_data->row()->template;
$this->load->view($page_content, $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
public function page($id) {
$page_data = $this->get_data->AllData('pages', $id);
$data['title'] = $page_data->row()->pagetitle;
$class = explode("/", $page_data->row()->template);
$data['body_class'] = $class[1];
$this->load->view('includes/header.php', $data);
if($class[1] == 'home') {
$this->load->view('templates/slider');
}
$this->load->view('templates/navigation.php');
if($class[1] == 'home') {
$data['slider'] = 'templates/slider';
}
$data['content'] = $page_data->row()->template;
$this->load->view('index', $data);
$this->load->view('templates/footer-form.php');
$this->load->view('includes/footer.php');
}
}
Now can anyone help me out to solve this issue one thing more when I comment the custom routes it is working perfectly but the home controller for the page which i tried to remove method and controller name it is coming as 404 not found
According to what i undertand, Set your default controller, to check your authentication first:
$route['default_controller'] = 'admin';
//so that you can check weather the user is logged in or not.
To access your second controller's index function:
$route['Home'] = "home";
To access your second controller's page($id) function:
$route['Home/Page/(:num)'] = "home/page/$1";
// where num id the ID you will be passing to the page function.
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"