【问题标题】:Why did Code igniter routes not working, Getting 404 Page Not Found?为什么 Code igniter 路由不起作用,找不到 404 页面?
【发布时间】:2019-06-09 14:57:42
【问题描述】:

我有一个表格可以提交房产详情。我在 CodeIgniter 中构建,我的表单中有一个 jquery 脚本来检查表单是否有效,并使用 dropzone 提交图像。 但是当我添加路线时它不起作用。

$route['image-upload']['post'] = 'property/uploadImages';

这里 控制器:Property.php 方法 。 : 上传图片()

uploadImages() :这里我上传来自 dropzone 拖放文件上传的图像。

我的 .htaccess 文件

<IfModule mod_rewrite.c>



    # Make sure directory listing is disabled
    Options +FollowSymLinks -Indexes
    RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$0 [L]

    # NOTICE: If you get a 404 play with combinations of the following commented out lines
    #AllowOverride All
    #RewriteBase /wherever/codeginiter/is

    # Restrict your site to only one domain
    # !important USE ONLY ONE OPTION

    # Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    #RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

    # Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
    #RewriteCond %{HTTPS} !=on
    #RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    #RewriteCond %{HTTP_HOST} (.+)$ [NC]
    #RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]

    # Remove index.php from URL
    #RewriteCond %{HTTP:X-Requested-With}   !^XMLHttpRequest$
    #RewriteCond %{THE_REQUEST}             ^[^/]*/index\.php [NC]
    #RewriteRule ^index\.php(.*)$           $1 [R=301,NS,L]

    # Keep people out of codeigniter directory and Git/Mercurial data
    # RedirectMatch 403 ^/(system|\.git|\.hg).*$

    # Send request via index.php (again, not if its a real file or folder)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    <IfModule mod_php5.c>
        RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

    <IfModule !mod_php5.c>
        RewriteRule ^(.*)$ index.php?/$1 [L]

    </IfModule>

</IfModule>



上传图片():

function uploadImages()
{
     $this->load->helper('url');
     if($this->input->post() != NULL ){


    $data = array();

    $config['upload_path'] = 'property-images/';
    $config['allowed_types'] = 'jpg|jpeg|png|gif';
    $config['max_size'] = '2048'; // max_size in kb

    foreach($_FILES['file']['name'] as $key => $value) {

        $config['file_name'] = $_FILES['file']['name'][$key];

        // Load upload library
        $this->load->library('upload',$config);
        // File upload
        if($this->upload->do_upload('file')) {

          // Get data about the file
           $uploadData = $this->upload->data();
           $filename = $uploadData['file_name'];
           $propertyId = 1;
           $imageData = array('path'=>$imgPath, 'propertyid'=>$propertyId);
           $result = $this->property_model->uploadImages($imageData);



         }

       }

       return $result;


}


当我调用 url http://www.cerdvillas.dev.cc/admin/image-upload/post 时,它显示“404 Page Not Found”

【问题讨论】:

    标签: php codeigniter post routes


    【解决方案1】:

    您需要在 .htaccess 文件中指定 Base Rewrite。此文件中的大部分代码也被注释掉了。 在您的 .htaccess 代码中使用它

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        #Removes access to the system folder.
        #Additionally this will allow you to create a System.php controller,
        #‘system’ can be replaced if you have renamed your system folder.
        RewriteCond %{REQUEST_URI} ^system.*
        RewriteRule ^(.*)$ /index.php?/$1 [L]
    
        #Checks to see if the user is attempting to access a valid file,
        #such as an image or css document, if this isn’t true it sends the
        #request to index.php
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        #This last condition enables access to the images and css folders, and the 
        robots.txt file
        RewriteCond $1 !^(index\.php|images|robots\.txt|css)
        RewriteRule ^(.*)$ index.php?/$1 [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule>
    

    考虑阅读mod_rewrite documentation 以了解有关 Apache mod_rewrite 规则的更多信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-20
      • 2013-08-12
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      相关资源
      最近更新 更多