在编码器中从视图调用函数不工作


calling function from view in codeigniter not working

我正在调用控制器函数从视图与基础url,它会到那个函数,但显示404页未找到。我使用的。htaccess是这个

<IfModule mod_rewrite.c>
  RewriteEngine On 
  RewriteCond %{REQUEST_FILENAME} !-f 
  RewriteCond %{REQUEST_FILENAME} !-d 
  RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
</IfModule>

config。

$config['index_page'] = '';

autoload is

$autoload['helper'] = array('form', 'url', 'security');

routes.php中的默认控制器是

$route['default_controller'] = 'prog_bar';

base_url是

$config['base_url'] = 'http://localhost:8080/jsLearning/prog_bar/';
视图

a href="<?php echo base_url().'file_func'; ?>">abc</a>
控制器

        <?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";
            }        
    }

我还错过了什么?

类名以大写字母开头;改变这个

class prog_bar extends CI_Controller {

class Prog_bar extends CI_Controller {

您的base_url是错误的。应该是

$config['base_url'] = 'http://localhost:8080/jsLearning/';

,现在在HTML中使用site_url()代替base_url(),如

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

base_url()只能用于资源链接,如图片文件,css文件等

更新.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

我觉得你的设置应该很好:

在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]

如果你仍然有错误,检查一下你的base_url链接到什么。

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

站点内的链接不使用基url。使用控制器名和方法名

<?php echo anchor("Pages/entry", "Post Entry"); ?>

什么是形式?它没有定义。它是一页吗?如果是,则没有视图地址。如果它是一个代码片段,你仍然需要一个地址

$this->load->view('prog_bar_view')

函数不会简单地显示视图或代码片段而不告诉它在哪里。如果你想在页面中包含表单,那么只需使用嵌套视图并在主(html)视图中加载你想要显示的视图,使用$this->load->view()