url的一个例子:http://www.example.com/search/search-keyword
我试图使这项工作,我删除了index.php使用。htaccess,搜索是控制器,我想在索引方法中传递一个参数。
这是当前的样子:
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
有什么建议吗?我似乎不能使它工作
- 您需要
index
段http://www.example.com/search/index/search-keyword
。 - 或者您需要使用路由
$route['search/(:any)'] = 'search/index/$1';
- 或者你可以查看remap
记住不要相信用户输入,特别是当你把它扔到你的url中时。最新版本的CI现在支持$_GET
变量,因此您可能希望使用它或flashdata。像O' Brien
这样简单的搜索项会给您一个致命的错误("您提交的URI中有不允许的字符")。
class Search extends CI_Controller{
function _remap($param) {
$this->index($param);
}
function index($param){
echo $param;
}
}
你应该可以访问:/search/123123:,页面会回显出"123123"或你放在那里的任何东西。
function _remap($parameter){
$this->_index($parameter);
}
试一试,告诉我们结果如何。
大多数这些解决方案将只工作,如果你只有一个单一的功能称为index()
在你的控制器。如果你的路由中有这个:
$route['search/(:any)'] = 'search/index/$1';
有了上面的路由,你的搜索功能会正常工作,但是如果你在同一个控制器中添加任何其他功能,它们都会被重定向到/search/index/$1
。
我能想到的唯一解决方案,允许你使用你想要的URL,同时仍然能够添加任何其他功能到你的搜索控制器是添加一种混乱的条件到你的路由文件。下面是它的样子和作用:
$request = $_SERVER['REQUEST_URI']; // Only add this for readability
if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) {
$route['search/(:any)'] = 'search/index/$1';
}
这基本上是在说"如果请求的URL不包含我的任何搜索控制器函数的名称,那么它必须用于索引,所以我们将激活索引的路由规则"。
这将允许您毫无问题地接受search/any_other_functions_you_have
的请求,并且仅在请求URI与它们中的任何一个都不匹配时才激活隐藏索引函数的规则。
这样做的一个副作用是你永远不会得到404。例如,如果用户输入了一个类似"yourdomain.com/search/something"的URL,他们希望它显示一个非搜索结果页面,他们不会得到一个404提示,提醒他们没有这样的页面,相反,应用程序会假设他们输入的是一个搜索词。然而,听起来这对你来说不是什么大问题,我不认为一个控制器不能返回404是一件可怕的事情。
你需要了解代码点火器url的工作方式,它基本上是这样的:
http://www.mysite.com/{控制器}/{函数}
所以你的url实际上是在搜索控制器中寻找一个名为"keyword"的函数。
你有多种选择。最简单的方法是将url更改为:
http://www.mysite.com/search/result/keyword那么这应该可以完美地工作:
class Search extends CI_Controller{
function result($param){
echo $param;
}
}
如果你真的想用原来的url,你可以使用与上面相同的代码片段,但也要打开"application/config/routes.php"并将其添加到底部。
$route['search/(:any)'] = "search/result";
或者,如果您想继续使用索引函数,您可以将其更改为
$route['search/(:any)'] = "search/index";
然后把你的类改成这样:
class Search extends CI_Controller{
function index($param=FALSE){
if($param == FALSE) {
//Show search box
} else {
//Show search results
}
}
}
不要忘记更新你的答案,如果你自己找出答案或接受别人的答案,如果它回答:)
我刚刚开始CI,这是我的解决方案,并在我的网站上实现了它。到目前为止,它对我有效,我的搜索查询已被谷歌索引为链接
Class Search extends CI_Controller{
function index(){
$search_item = $this->input->post('search_box'); // this is from the input field
redirect(base_url()."search/q/".url_title($search_item));
}
// i've redirected it to the "search" controller then :
function q($search_item = null){// process search
// load the view here with the data
}
}
这是我的解决方案:
From CI userGuide
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
我这样创建了控制器:
class Shortener extends CI_Controller {
function shortener() {
parent::__construct();
}
function index($getVar) {
echo "Get var: " . $getVar;
}
function config() {
echo "another function";
}
function _remap($method) {
if($method == "config") {
$this->$method();
}
else {
$this->index($method);
}
}
}
希望这对某人有帮助。
BUG:如果你调用/shortener _remap函数传递"index"给index()函数…它可以通过添加if条件或其他东西来解决
您应该使用CI URI类。http://codeigniter.com/user_guide/libraries/uri.html
class Search extends CI_Controller{
function index($param){
//echo the search-keyword
echo $this->uri->segment(2);
}
}
最简单的方法就是使用uri段:
class Search extends CI_Controller{
function index(){
$param=$this->uri->segment(2);
echo $param;
}
}
或者像Madmartigan说的那样,使用路由,这可能是更好的方法。
我还必须检查是否传递了一些变量-所以这是我采取的解决方案。
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Story extends CI_Controller{
function story() {
parent::__construct();
}
function _remap($parameter){
$this->_index($parameter);
}
public function _index($story_id) {
if($story_id == 'index'){
//No variable passed
redirect('auth', 'refresh');
}else{
$data['title'] = "Story";
$this->load->view('story', $data);
}
}
}
希望这能帮助到一些人!
谢谢。这段代码对我有用。如果参数是数字
Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
但是,如果参数是字符串(http://[::1]/note/getnote/index/fg => http://[::1]/note/getnote/fg)。我像下面这样使用_remap():
<?php
类Getnote扩展CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
public function index(){
}
public function _remap($keyname)
{
$this->load->model('Laydb');
$data = $this->Laydb->getNote($keyname);
$proListArray['patitle'] = "Edit notepad";
$this->load->view('top', $proListArray);
$this->load->view('editblog', $data);
$this->load->view('bottom');
}
}
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
在routes.php $routes['search/(:any)'] = 'search/index/$1';
的
我终于找到了一个解决方法,因为我不能简单地将post变量放入URL。
我所做的是创建另一个函数,然后将其重定向到该函数。
class Search extends CI_Controller{
function index(){
$search_item = $this->input-post('search_item');
redirect("search/q/".url_title($search_item));
}
function q($key){
// process search
}
}