Codeigniter 不从 URL 获取参数
我有一个函数需要像 CI 那样从 URL 中提取参数。但它并没有这样做。我的网址是domain.com/lasers/en/acme。
我的激光类是:
class Lasers extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('products_model');
$this->load->model('common_model');
$this->load->model('select_country_model');
$this->load->model('markets_materials_model');
}
function index($lang = NULL, $laser = NULL)
{
$query = $this->products_model->get_product_content($laser, $lang);
}
模型在构造函数中加载。我需要的 $lang 是“en”,我需要的 $laser 是“acme”。那么为什么这不起作用呢?函数中的参数顺序正确,所以我看不出有什么问题。
I have a function that needs to pull arguments from the URL like CI is supposed to do. But it's not doing it. My URL is domain.com/lasers/en/acme.
My class Lasers is:
class Lasers extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('products_model');
$this->load->model('common_model');
$this->load->model('select_country_model');
$this->load->model('markets_materials_model');
}
function index($lang = NULL, $laser = NULL)
{
$query = $this->products_model->get_product_content($laser, $lang);
}
The model is loaded in the constructor. The $lang I need is "en" and the $laser I need is "acme". So why isn't this working? The arguments in the function are in the correct order, so I can't see what's wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法将参数传递给控制器的
index
方法。默认情况下,如果您访问
domain.com/lasers/en/acme
控制器正在lasers<中查找,则 /code> 控制器,用于名为
en
..(不存在)的方法,并尝试将acme
的单个参数传递给它。有几种解决方案,可能是最简单的方法是使用不同的方法(不是索引)然后使用路由使 URL 正常工作。
将类似的内容添加到您的
config/routes.php
然后使用这样的方法而不是
index
:.. 或者您可以使用 _remap 覆盖默认行为
By default you cant pass arguments to the
index
method of a controllerif you go to
domain.com/lasers/en/acme
it is looking in thelasers
controller for a method calleden
.. (which doesn't exist) and trying to pass a single parameter ofacme
to itTheres a few solutions, probly the easiest is to use a different method (not index) then use routes to make the URLs work.
add something like this to your
config/routes.php
Then use a method like this instead of
index
:.. OR you could use _remap to override the default behaviour
如果你写“domain.com/lasers/index/en/acme”,它可以工作吗?
如果您编写domain.com/lasers/en/acme,它将查找“En”函数,$lang 为“acme”,而$laser 保留为NULL。
Does it work if you write "domain.com/lasers/index/en/acme"?
If you write domain.com/lasers/en/acme, it will look for the "En" function, $lang being "acme", and $laser remaining NULL.