从控制器加载函数改为加载控制器的索引函数


Loading function from controller loads controller's Index function instead

我有一个jQuery load()函数,它调用控制器函数,但从控制器索引函数加载load->view(),导致整个页面被加载到div中。

HTML "mypage.php"

 <h1 class="output"></h1>
 <button class="button" onclick="accept(); return false;"> Accept</button>

.JS:

function accept(){
     $('.output').load('mycontroller/accept');
}

PHP 控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mycontroller extends CI_Controller {

  function index() {
         $this->home();
  } 

  function home() {
         $this->load->view('user/home');
  } 

 /// load page where button is
  function mypage(){
          $this->load->view("user/mypage");

}
 function accept(){
         echo 'HELLO PHP controller';
     }

}

如果我从控制器中删除索引函数,它不会发生,但控制器中的"接受"函数仍然无法运行:(

我有一个类似的页面"主页",它在索引函数中调用。因此http://localhost:8888/mysite/mycontroller/它具有相同类型的load()并且可以工作。但是,如果我在"/mycontroller"之后附加/indexhome,我会遇到与另一个页面"mypage"相同的问题。 所以就像load()函数似乎只有在指向控制器本身而没有任何功能时才起作用。这和.htaccess有什么关系吗设置??

HTACCESS:

Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !index.php
    RewriteRule (.*)'.php$ index.php/$1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>

将不胜感激任何帮助。提前谢谢。

请使用为CodeIgniter量身定制的.htaccess,重写库很重要,我想你需要使用RewriteBase /mysite。我一年/两年前从网上得到它不记得来源(会搜索它),来源,另一个在要点上。

<IfModule mod_rewrite.c>
# mod_rewrite rules
RewriteEngine on
# The RewriteBase of the system (if you are using this sytem in a sub-folder).
 RewriteBase /
# This will make the site only accessible without the "www."
# (which will keep the subdomain-sensive config file happy)
# If you want the site to be accessed WITH the "www."
# comment-out the following two lines.
#RewriteCond %{HTTP_HOST} !^localhost$ [NC]
#RewriteRule ^(.*)$ http://localhost/$1 [L,R=301]
# If a controler can't be found - then issue a 404 error from PHP
# Error messages (via the "error" plugin)
# ErrorDocument 403 /index.php/403/
# ErrorDocument 404 /index.php/404/
# ErrorDocument 500 /index.php/500/
# Deny any people (or bots) from the following sites: (to stop spam comments)
# RewriteCond %{HTTP_REFERER} nienschanz'.ru [NC,OR]
# RewriteCond %{HTTP_REFERER} porn'.com
# RewriteRule .* - [F]
# Note: if you are having trouble from a certain URL just
# add it above to forbiden all visitors from that site.
# You can also uncomment this if you know the IP:
# Deny from 192.168.1.1
# If the file is NOT the index.php file
RewriteCond %{REQUEST_FILENAME} !index.php
# Hide all PHP files so none can be accessed by HTTP
RewriteRule (.*)'.php$ index.php/$1
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
#<ifModule mod_expires.c>
#  <filesmatch "'.(ico|flv|jpg|jpeg|png|gif|js|css|swf)$">
#       ExpiresActive on
#       ExpiresDefault "access plus 1 year"
#   </filesmatch>
#</ifModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/xhtml text/html text/plain text/xml
text/javascript application/x-javascript text/css
</IfModule>

JavaScript

为 JavaScript 范围定义base_url,以便您可以在 JavaScript 文件中使用base_url

要么创建一个帮助程序,要么在页面顶部每次加载时在每个页面中分配base_url。

<script>
    var base_url = <?= base_url()?>
</script>

将上面的代码放在某种始终加载的视图中(在执行任何其他 JavaScript 代码之前)。

将您的 JavaScript 代码更改为此代码

function accept() {
    $('.output').load(base_url + '/mycontroller/accept'); //fix leading slash by inspecting
}