Reputation: 227
I couldn't get the parameter value from url in codeigniter.
For instance: localhost/log/job/php
here log/
is my folder, job/
is my controller and php
is my parameter.
I want to get this parameter in controller 'job'. How can I do that?
Upvotes: 6
Views: 22032
Reputation: 23
Use:
$param = $this->uri->segment(3);
add .htaccess on your folder (on your case is "log"
) :
RewriteEngine On
RewriteBase /log/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /log/index.php/$1 [L]
Upvotes: 0
Reputation: 196
You can use $this->uri->segment(n);
You need to do some changes in route in order to allow the code igniter to receive the parameter in your controller
$route['uri-(:any)/(:num)'] = "controller/function/$1/$2";
OR
$route['uri-(:any)'] = "controller/function/$1";
in your controller do some changes
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class controller extends CI_Controller
{
function function($parameter1 = null, $parameter2 = null)
{
........
}
}
refer this http://www.codeigniter.com/userguide2/libraries/uri.html
Upvotes: 5
Reputation: 1072
You get it by this:
$this->uri->segment(n);
where n = 1 for controller, n = 2 for method and n = 3 for parameter and so on.
You need n = 3 to get parameter.
In your path localhost/log/job/php
, your method name is missing.
Even if your method name is index
then you route will be localhost/log/job/index/php
In case if you need to remove index.php from url then you will get parameter using localhost/log/index.php/job/index/php
To remove index.php you need to create .htaccess file by following these steps:
Create a .htaccess file where index.php file is located with content
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Make sure that apache can access this .htaccess file. To do this edit apache configuration file. If you use ubuntu, then it is /etc/apache2/sites-available/default
and then change AllowOverride none
to AllowOverride all
for directory and www directory.
<Directory />
Options FollowSymLinks
AllowOverride all
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
Then enable mod rewrite if you don't have it, with the following command:
`sudo a2enmod rewrite`
Finally do not forget to restart apache.
Hope this will help.
Upvotes: 2
Reputation: 5793
Assuming your parameter will be in the end always:
$segs = $this->uri->segment_array();
echo end($segs);
EDIT: For the clarify other essentials. First you need the setup your application/config/routes.php
:
$route['Youruri-(:any)/(:num)'] = "yourcontroller/yourfunction/$1/$2";
$route['Youruri-(:any)'] = "yourcontroller/yourfunction/$1";
In the controller application/controllers/yourcontroller.php
you need to define a function:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Yourcontroller extends CI_Controller {
function yourfunction($brand = null, $page = null) {
// http://www.yoursite.com/Youruri-Microsoft
// or http://www.yoursite.com/yourcontroller/yourfunction/Microsoft
// do some stuff for get products of this $brand (Microsoft)
if($page != null) {
// http://www.yoursite.com/Youruri-Intel/2
// or http://www.yoursite.com/yourcontroller/yourfunction/Intel/2
// do some other stuff get products of this $brand (Intel) of $page (2)
}
}
}
Upvotes: 2
Reputation: 41786
You might use $this->uri->segment(n)
.
http://www.codeigniter.com/userguide2/libraries/uri.html
Upvotes: 6