简体   繁体   English

将变量传递给默认的Codeigniter控制器

[英]Pass variable to default codeigniter controller

I have my codeigniter setup with a default controller. 我使用默认控制器设置了代码初始化程序。 It's accessible as follows: 可通过以下方式访问:

site.com/index.php 

But it's actually: 但这实际上是:

site.com/project/index

Where index is the default function. 其中index是默认功能。

I would like to do the following: 我要执行以下操作:

site.com/project7362

But it actually wants: 但是它实际上想要:

site.com/project/index/project7362

Where project name is a variable that is pass into the index function. 项目名称是传递给索引函数的变量。 But by default it looks for project-name as a controller. 但是默认情况下,它会寻找项目名称作为控制器。 Is there a way to avoid this? 有办法避免这种情况吗?

Essentially what I'm hoping to accomplish is to pass a variable directly after the domain name. 本质上,我希望完成的工作是在域名后直接传递一个变量。 A user may create a project, and I want that project to be accessible at domain.com/project_id 用户可以创建一个项目,并且我希望可以在domain.com/project_id上访问该项目。

"Essentially what I'm hoping to accomplish is to pass a variable directly after the domain name. A user may create a project, and I want that project to be accessible at domain.com/project_id" “基本上,我希望完成的是在域名后直接传递一个变量。用户可以创建一个项目,并且我希望该项目可以在domain.com/project_id上访问”

so another way to do this would be to have it like. 因此,另一种方法是让它像。

domain.com/project/id

this will give you much more flexibility later in your routes for adding different features. 这样可以在以后添加更多功能的过程中为您提供更大的灵活性。
in config/routes: 在配置/路由中:

$route['project/(:any)'] = 'project/view/$1'; 

in your project controller 在您的项目控制器中

    function view($id) {

    // clean it
    $id = htmlspecialchars($id) ;

   if ( ! $project = $this->members->returnProjectBy($id) {

       $this->showNoResultsFor($id) ; } 

   else { $this->show($project) ;  } 

   }    

OR -- another way to do this would be to put your defined routes first, and then have project be last (because it requires searching on whatever is there) 或-另一种方法是将您定义的路线放在首位,然后将项目放在最后(因为它需要搜索那里的任何东西)

$route['home'] = 'page/home'; 
$route['contact'] = 'contact'; 
// etc etc so you first define your hard coded routes, and then if its not any of those
// you do a search on whatever the value is to find a project 
$route['(:any)'] = 'project/view/$1';

so then your link could be 所以你的链接可能是

domain.com/id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM