【问题标题】:Codeigniter 3 - how to remove the function name from URLCodeigniter 3 - 如何从 URL 中删除函数名称
【发布时间】:2017-12-13 01:45:34
【问题描述】:

我的网址是:

example.com/controller/function/parameter
=> example.com/category/index/category_name

我需要:

example.com/category/category_name

我已经尝试了 Stackoverflow 提供的几种解决方案,但都没有奏效。它要么重定向到家,要么重定向到404 page not found

我尝试过的选项是:

$route['category'] = "category/index"; //1

$route['category/(:any)'] = "category/index"; //2

$route['category/(:any)'] = "category/index/$1";  //3

另一条路线是:

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

htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

在配置文件中我有:

$config['url_suffix'] = '';

【问题讨论】:

  • $route['category/(:any)'] = "category/index/$1"; //3 这一定可以正常工作。我试过了。并得到正确的回应
  • 我认为您只需要路线#3。你能展示你的类别控制器吗?

标签: php .htaccess codeigniter


【解决方案1】:

我不知道你为什么不能让它工作。

这是我创建的一些测试代码来检查... 这是使用 CI 3.1.5。

.htaccess - 与您拥有的相同...

控制器 - Category.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function index($category_name = 'None Selected') {
        echo "The Category name is " . $category_name;
    }
}

routes.php

$route['category/(:any)'] = "category/index/$1";  //3 - this works

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

测试网址

/category/ 输出: The Category name is None Selected

/category/fluffy-bunnies 输出: The Category name is fluffy-bunnies

试一试,看看是否能找到问题。

【讨论】:

  • localhost/campnew/category/garden-tents-2380 进入主页...而链接localhost/campnew/category/index/garden-tents-2380 在页面顶部显示“类别名称为 garden-tents-2380”跨度>
  • @TimBrownlaw 默认路由应该是第一条,但是当你有某种通配符占位符时,它应该最多适用于结束行。
  • @Tpojka 理想情况下是的。我只是把它放在顶部以使其更加突出并确保它在测试时首先得到...记住它只是调试/测试代码...
  • 我可以(或者您也可以)在第一眼看到它。我只是告诉建议的代码应该以没有机会产生与第一个问题相关的其他最终错误(不是显式或隐式)的方式给出。
【解决方案2】:

我认为您的 .htaccess 文件中有错误。请在下面找到.htaccess 文件的代码。

您可以使用RewriteBase 为您的重写提供基础。

RewriteEngine On
RewriteBase /campnew/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

在控制器中你的方法。

public function index($category_name = null) {
    $this->load->model('category_model');

    $data = array();

    if ($query = $this->category_model->get_records_view($category_name)) {
        $data['recordss'] = $query;
    }
    if ($query2 = $this->category_model->get_records_view2($category_name)) 
    {
        $data['recordsc2'] = $query2;
    }

    $data['main_content'] = 'category';
    $this->load->view('includes/template', $data);
}

在模型文件中

public function get_records_view($category){
    $this->db->where('a.linkname', $category); 
}

如果它不起作用,请告诉我。

【讨论】:

  • 它仍然重定向到主页
  • 您是否在config/routs.php 文件中添加了$route['category/(:any)'] = "category/index/$1"; 行?
  • 还有你如何在你的模型函数中传递参数..?我在你的控制器功能中看不到它。
  • 在模型函数中使用 where('a.linkname', $this->uri->segment(3));从 db 获取相关详细信息并在该类别下的视图页面中显示产品
  • 让我们在聊天中讨论:chat.stackoverflow.com/rooms/148714/…
猜你喜欢
  • 2016-11-02
  • 2011-11-28
  • 2020-07-03
  • 2014-12-16
  • 2017-04-10
  • 1970-01-01
  • 1970-01-01
  • 2015-09-28
  • 1970-01-01
相关资源
最近更新 更多