[英]How to remove controller and function name from base url in Codeigniter
[英]Codeigniter 3 - how to remove the function name from URL
我的網址是:
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'] = '';
我不知道為什么你不能讓它工作。
這是我創建的一些測試代碼,用於檢查...這是使用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;
測試URL
/category/
output: The Category name is None Selected
/category/fluffy-bunnies
輸出: The Category name is fluffy-bunnies
玩一玩,看看你是否能找到問題。
我認為你的.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]
在Controller中你的方法。
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);
}
如果它不起作用,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.