问题
Base URL: http://localhost/f1/
and I am passing parameter in URL like: http://localhost/f1/user1
and I am trying to print
function index(){
echo $this->uri->segment(2);
}
I want to print User1
in the controller. How to achieve this?
回答1:
Config your rout and add this:
$route['Controller_Name/(:any)'] = 'Controller_Name/index/$1';
Here you go with your controller
class Controller_Name extends CI_Controller {
function index($prm){
echo $prm;
}
}
Have fun.... you can have how many uri you want....
回答2:
Read here 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;
}
}
回答3:
After base_url
segemnts/parameter can get like this
Make sure base_url
and URL helper is set in the application/config/config.php
and application/config/autoload.php
file respectively
Suppose, calling a function with a anchor tag
<a href="<?= base_url('HomeController/index/'.$url1)?> ">Index</a>
In the above line, HomeController
is controller name, index
in the function name and $url1
is the parameter
class HomeController extends CI_Controller {
public function index($url1){
echo $url1;
}
}
Also, $this->uri->segment()
can be used.
回答4:
What i understood from your question is that you want a remap without using your method and pass the parameter directly after your controller?
You can user _remap
and check if it matched a method process it normally or pass it to your default method directly and now you won't need to use index
in your url as you intended.
public function _remap($method)
{
if ($method === 'some_method_in_your_controller')
{
$this->$method();
}
else
{
$this->index($method);
}
}
Now lets say your url is like this http://localhost/controller/parameter
then if this parameter matches a method it will call that method if not it will pass it as a parameter to you index
.
回答5:
define your controller in config/routes.php.it will call your index function by default.
$route['default_controller'] = 'controllername';
回答6:
check whether you loaded the url helper. Only then it will work as expected. Either auto-load it or call it in the controller.
来源:https://stackoverflow.com/questions/53944667/how-to-pass-parameter-after-baseurl-in-codeigniter