基本网址: http://localhost/f1/
我正在 URL 中传递参数,例如: http://localhost/f1/user1
我正在尝试打印
function index(){
echo $this->uri->segment(2);
}
我想在控制器中打印User1
。如何实现这一点?
配置你的路由并添加以下内容:
$route['Controller_Name/(:any)'] = 'Controller_Name/index/$1';
在这里,您可以使用控制器
class Controller_Name extends CI_Controller {
function index($prm){
echo $prm;
}
}
玩得愉快。。。。你可以拥有你想要多少个 URI。
在这里阅读 https://www.codeigniter.com/userguide3/general/controllers.html -> Passing URI Segments to your methods
/products/shoes/sandals/123
<?php
class Products extends CI_Controller {
public function shoes($sandals, $id)
{
echo $sandals;
echo $id;
}
}
base_url
之后,语义/参数可以像这样
确保分别在application/config/config.php
和application/config/autoload.php
文件中设置了base_url
和 URL 帮助程序
假设,调用一个带有锚标记的函数
<a href="<?= base_url('HomeController/index/'.$url1)?> ">Index</a>
在上行中,HomeController
是控制器名称,index
是函数名称,$url1
是参数
class HomeController extends CI_Controller {
public function index($url1){
echo $url1;
}
}
此外,可以使用$this->uri->segment()
。
我从您的问题中了解到的是,您希望在不使用方法的情况下进行重映射,并将参数直接传递给控制器之后?
您可以用户_remap
并检查它是否与方法正常处理或直接传递给默认方法,现在您无需按预期在 url 中使用index
。
public function _remap($method)
{
if ($method === 'some_method_in_your_controller')
{
$this->$method();
}
else
{
$this->index($method);
}
}
现在假设您的 url 是这样的http://localhost/controller/parameter
那么如果此参数与方法匹配,它将调用该方法,如果不是,它将将其作为参数传递给您index
。
在config/routes中定义控制器.php默认情况下,它将调用索引函数。
$route['default_controller'] = 'controllername';
检查是否加载了 url 帮助程序。只有这样,它才会按预期工作。自动加载它或在控制器中调用它。