简体   繁体   中英

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

<a href="<?php echo site_url('prog_bar/file_func'); ?>">abc</a>

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:

<a href="<?php echo base_url(); ?>file_func">abc</a>

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

<a href="<?php echo base_url(); ?>">Base Url</a>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM