calling function from view in codeigniter not working - php

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()

Related

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 Codeigniter controller method not working

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

Subdomain does not work for CodeIgniter

I've a Subdomain under a maindomain. I want to install CodeIgniter under subdomain. But subdomain does not work for CodeIgniter default route.
Like my subdomain is demo.example.com not working. Normally core php is working for this subdomain but CodeIgniter not.
But when I call demo.example.com/welcome then working. Here welcome is a Controller name.
Can anyone help me what is my wrong ?
My Base URL is:
$config['base_url'] = 'http://demo.example.com';
My .htaccess is
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
My default controller(welcome.php) is
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
echo 'Welcome Subdomain';
}
}
Your controller name is Welcome.php as a public_html/demo/application/controllers/Welcome.php.
Please change instead Welcome.php to welcome.php
Set instead $route['default_controller'] = "Welcome" to $route['default_controller'] = "welcome" in routes.php
If you have CodeIgniter install in your main domain add RewriteBase / in your .htaccess

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.

error while calling a controller in codeigniter (php)?

I made a website in codeigniter . it is working fine on local host and on the live server it is opening the default controller normally but when i m tring to call other controller then i m getting error of No input file specified. everything is working fine on local host .
class Contact_us extends CI_Controller {
public function index()
{
echo $data["header"] = $this->load->view('header');
//$this->load->view('index',$data);
}
}
I am calling my controller like mysitename/index.php/contact_us
it works with change in htaccess file like
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
and put $config['index_page'] = ""; in the config file
so now my site url becomes like mysitename/contact_us

Categories