Calling Codeigniter controller method not working - php

I'm starting to develop with CI, I'm trying to call another controller that I call LOGIN but it gets me the error "NOT FOUND", whereas if I put my controller in default_controller, it works, I already have read the other forum about this problem but it does not solve my case,
class Login extends CI_Controller {
public function index() {
redirect("/welcome/index");
$this->load->view("login");
}
}

Possible solutions
1. Please add .htaccess to the codigniter
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2. Can create a routing for login controller then you can call directly
3. Add $config['index_page'] = 'index.php'; in config.php

Solution -1
class Login extends CI_Controller {
public function index() {
redirect("index.php/welcome/index");
$this->load->view("login");
}
}
Solution-2
application/config/config.php file you not remove index.php
$config['index_page'] = 'index.php';
change it
$config['index_page'] = '';
and using .htaccess index.php remove.
Reference link: index.php from URL

Related

codeingter3 HMVC 'default_controller' is not working

I am using CI3 HMVC in my project. Now, I am facing problem with routing. I want user to type www.demosite.com and it would automatically call my home module. I do not want to show like www.demosite.com/home. I want to show the url like www.demo.com. for this, I set default controller in application/config/routes, like this, as follows;
$route['default_controller'] = "home";
also in my content module, I added a route folder where I wrote
$route['home'] = 'home';
Here is my .htaccess
AddType text/x-component .htc
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !(index\.php|assets/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This is my home controller
class Home extends MX_Controller{
function __construct(){
parent::__construct();
}
function index($stub=""){
$baseUrl=base_url();
$this->load->helper("url");
echo $this->_showHomepage();
}
}
However, when I run this. I got 404 error. What can i do to solve this problem? Thanks in advance.
HMVC it should be like that
This only takes a method no directories are allowed
so the default controller is under controllers
$route['default_controller'] = 'pages/pages/view';
$route['default_controller'] = 'pages';
index is the default method that is called.
$route['home'] = 'authentication/home/index';
$route['home'] = 'authentication/home';
And than you need to change your .htaccess file to match that urls
URI Routing : https://codeigniter.com/user_guide/general/routing.html#examples
Update :
(defined('BASEPATH')) OR exit('No direct script access allowed');
class Site extends MY_Controller {
function __construct() {
parent::__construct();
}
function index() {
$this->load->view('url');
}
}
I solved it. I moved the Home folder from Modules folder to Controller folder and then, it just started to work

Duplicate content issue due to Codeigniter controllers

I am assisting with a Codeigniter website and don't have a background with it. It has been noted that there is duplicate page content. When I dug in, they are not actual pages, but controllers pointing to the same views. I attempted to set up 301 redirects in an .htaccess file in the root of the website, but it is not taking. In the application/config/routes.php, the following is noted:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
For example, there is a page http://mywebsite.com/weddings, and then http://mywebsite.com/Home/weddings was another controller that pointed to the same view. I attempted to add a 301 redirect from Home/weddings after removing the additional controller pointing to it, but now Home/weddings just redirects to the home page, as it is set up with the 404 override. I hope I have provided enough information for some assistance.
.htaccess file:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1
!^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^Home/weddings/$ /weddings
Home.php is as follows:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Admin_model');
$this->load->model('Home_model');
$this->load->helper('captcha');
$this->load->library('recaptcha');
}
public function weddings()
{
$this->load->view('Template/Home/front_header');
$this->load->view('Home/weddings');
$this->load->view('Template/Home/front_footer');
}
Do not touch .htaccess. You have two options.
Create a controller Weddings.php and in your index.php return a
view for weddings page.
The other option is to use Home.php controller and then add weddings method, the method should return the weddings page view. Your route should look like below:
$route['default_controller'] = 'Home';
$route['404_override'] = 'Home';
$route['/weddings'] = 'home/weddings';
Hope this will help. Thank you
Edit
In your Weddings.php controller constructor check if there exist a "Home" and return 404 error. See below
function __construct()
{
parent::Controller();
if($this->uri->uri_segment(1) == 'Home') {
show_404();
}
}

calling function from view in codeigniter not working

I am calling controller function from view with base url it's going to that function but showing 404 Page Not Found.
.htaccess I am using is this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
config.php
$config['index_page'] = '';
autoload is
$autoload['helper'] = array('form', 'url', 'security');
default controller in routes.php is
$route['default_controller'] = 'prog_bar';
base_url is
$config['base_url'] = 'http://localhost:8080/jsLearning/prog_bar/';
view
a href="<?php echo base_url().'file_func'; ?>">abc</a>
controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class prog_bar extends CI_Controller {
public function index()
{
$this->load->view('prog_bar_view');
}
public function file_func(){
echo "form";
}
}
what else I am missing?
Class name should be started with capital letter,
Change this
class prog_bar extends CI_Controller {
to
class Prog_bar extends CI_Controller {
Your base_url is wrong. It should be
$config['base_url'] = 'http://localhost:8080/jsLearning/';
and now in HTML use site_url() instead base_url() like
abc
base_url() should be used only for resources link like image file, css file etc
Update your .htaccess by following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I think you should be great with settings below:
In Config.php:
$config['base_url'] = 'http://localhost:8080/jsLearning/';
In Your View:
abc
In Your Controller:
class Prog_bar extends CI_Controller {
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
If you have still error, check to see what your base_url link to..Create a link
Base Url
to see in your browser where it links.
You do not use the base url for links within the site. Use the controller name and the methods name
<?php echo anchor("Pages/entry", "Post Entry"); ?>
What is form? It is not defined. Is it a page? if so, you have no view address for it. If its a snippet, you still need an address
$this->load->view('prog_bar_view')
a function will not simply show a view or snippet without being told where it is. If you are trying to include a form in a page, then just use nested views and in your main(html) view load the view you are trying to show with $this->load->view()

CodeIgniter setting up routing properly

I'm trying to setting up my codeigniter routing to make an authentication form for an admin panel, but the routing is not working. I'm a beginner with CodeIgniter and I think I'm missing something.
In my server I put the CodeIgniter fiels inside folder in my root directory, so it can be accessed like this:
/localhost/ci
In my config.php I set my base url and remove the index page to remove index.php:
$config['base_url'] = 'http://localhost/ci/';
$config['index_page'] = '';
Also I have edited the .htaccess to remove the index.php like CodeIgniter documentation says, so it looks like this:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my routes config file looks like this. I set my default page and a route to my Auth controller:
$route['default_controller'] = 'auth';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['auth/login'] = "auth/login";
This is my Controller:
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
// load libraries
}
function index() {
if (! $this->ion_auth->logged_in()) {
// redirect them to the dashboard page
redirect(base_url('auth/login'), 'refresh');
} else {
// show the dashboard page
redirect(base_url('dashboard'), 'refresh');
}
}
function login() {
$this->load->view('login');
}
When I enter in the web-browser http://localhost/ci/ it redirects me to http://localhost/ci/auth/login, but then a 404 Not Found error ir raised. What am I missing? I'm a bit confused at this point, thanks.
Try changing your .htaccess from:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
To
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

Why this controller is not working in code-igniter version 3.0.1?

class Users extends CI_Controller{
public function index(){
echo 'hello';
}
}
I kept the file name as Users.php, still its not working.
Please Consider about Codeigniter naming conversion.
and
in config.php
$config['base_url'] = '';
$config['index_page'] = '';
place .htaccess outside application folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and
in routes.php
$route['default_controller'] = "users";//default controller name
and in URL
http://localhost/<path to file>/project_name/users
Note : You cant access URL with any file extensions like .php or .html.
start contract function in your class
class Users extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
echo 'hello';
}
}
And run the url as example.com/index.php/users/
This might help you out:
-> app/config/routes.php
$route['default_controller'] = 'pages/homePage';
-> app/controllers/Pages.php (Note the capital P on the file)
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Pages extends MY_Controller {//Note the capital Class name
public function homePage()
{
$this->load->view("pages/homepage.php");
}
}
Also to get rid of the index.php do the following :
-> app/config/config.php
$config['index_page'] = '';
Create a file called ".htaccess" outside the app folder and put the following code in it:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
This HTaccess is from another PHP framework called Laravel, it seems to be working better than the standard CI's HTaccess that they provide on their website.
I hope this helps you out.

Categories