来自控制器的加载功能加载控制器的索引功能

时间:2014-12-13 01:28:28

标签: php jquery codeigniter

我有一个调用控制器函数的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';

     }


}

如果我从控制器中删除索引功能,它不会发生,但仍然是&#34;接受&#34;来自控制器的功能不会运行:(

我有一个类似的页面&#34; home&#34;在索引函数中调用。因此http://localhost:8888/mysite/mycontroller/它具有相同类型的load()并且它有效。但是,如果我在&#39; / mycontroller&#39;之后添加/indexhome,我的问题与另一页&#34; mypage&#34;相同。因此,当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>

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

1 个答案:

答案 0 :(得分:1)

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

<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
}