这是url的一个例子:http://www.example.com/search/search-keyword
我试图使这项工作,我使用.htaccess删除index.php,搜索是控制器,我想在索引方法中传递一个参数。
这是目前的样子:
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
有什么建议?我似乎无法使其发挥作用
我还必须检查是否传递了一些变量 - 所以这是我采取的解决方案。
<?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
class 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');
}
}
我终于找到了一个解决方法,因为我不能简单地将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
}
}
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”这样的网址,并且他们希望它显示非搜索结果页面,那么他们将无法获得404警告他们没有这样的页面,而应用程序将假设他们键入的是搜索词。然而,听起来这对你来说并不是什么大问题,我也不认为让一个控制器无法回复404这是一件非常糟糕的事情。
你需要了解代码点火器网址的工作方式,它基本上是这样的:
http://www.mysite.com/ {控制器} / {}函数
那么你的url实际上是在搜索控制器中看到一个名为“keyword”的函数。
你有多种选择。最简单的方法是将网址更改为:
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
}
}
这是我的解决方案:
来自CI用户指南
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 segments:
class Search extends CI_Controller{
function index(){
$param=$this->uri->segment(2);
echo $param;
}
}
或者像Madmartigan说的那样,使用路线,这可能是更好的方法。