CodeIgniter : Routing URL on Codeigniter - php

I just learned about routing in CodeIgniter and now I am lil bit confuse.
I have an URL like this : http://localhost/norwin/list_group/get_product_by_group/1
Which is:
list_group is controller,
get_product_by_group is method,
and '1' is passing parameter.
I want to minimize the URL with route. So I can have an URL like :
http://localhost/norwin/group/'group_name'
And this is my code on route.php :
$route['group/(:any)'] = 'list_group/get_product_by_group/$1';
this is my controller :
public function get_product_by_group($group_name)
{
if($this->uri->segment(3))
{
$this->load->database();
$data['product_data'] = $this->group_model->list_product_by_group($group_name);
$this->load->view('fend/list_product', $data);
}
else
{
redirect('list_group');
}
}
And this is my code on view which call the controller :
<?= base_url('group/'. $value->group_name);?>
my problem is : I can not get any result of the route, it always send me to 'list_group'.
Maybe someone here have a good approach to do this.
Thanks for your help.

You are being redirected because $this->uri->segment(3) is not returning anything as your URL http://localhost/norwin/group/'group_name' have only 2 segments.
Codeigniter $this->uri->segment(n); works in the following way.
URL: http://localhost/norwin/group/group_name/some_test
it will return
$this->uri->segment(1); // group
$this->uri->segment(2); // group_name
$this->uri->segment(3); // some_test
you have to change $this->uri->segment(3) to $this->uri->segment(2)

Related

get uri segments from default controller codeigniter

i have codeigniter setup to use "home" as the default controller which is to display homepage
i tried to run the index function within home controller to collect the uri segments to determine whether or not to redirect or display home page but it seems to think that the uri segment is another controller mysite.com/segment1
function index(){
$url1 = $this->uri->segment(1);
if(!empty($url)){
echo $url1;
}
else{
$this->load->view('home_page');
}
}
You are setting $url1 as the segment then testing if $url is empty or not.
From your code, $url will always be empty.
Make sure you keep an eye on what you call your variables.
So what you have now...
function index(){
$url1 = $this->uri->segment(1);
if(!empty($url)){ // $url will always be empty as it's not defined.
echo $url1;
}
else{
$this->load->view('home_page');
}
}
What you should have...
function index(){
$url = $this->uri->segment(1);
if(!empty($url)){
echo $url;
}
else{
$this->load->view('home_page');
}
}
Then it will do as you intended.
You can set default controller to home. Then to be able to read $this->uri->segment(3); you nead to pass thet uri via URL mysite.com/controller/function/uri-segment(3). If you hav an Index function in home controller mysite.com and you want to pass a value of 11 via uri your url shuld look like this:
mysite.com/home/index/11
For this to work i had to change the routes to
$routes[('any')] = "New_controller"; //created a new controller
in "New_controller" i could then define the first segment i want to get
function index(){
// define segments
$url = $this->uri->segment(1);
if(!empty($url)){
echo $url;
}
and of course my
$routes['default_controller'] = "home";
function index(){
// home controller
$this->load->view('home_page');
}
Now when i go to mysite.com it takes me to the homepage or if i go to mysite.com/12345 it takes me to the new controller page where i can run a query on the segment

Redirect user to a self-made 404 page when users input wrong arguments

I'm using Codeigniter version 3.0.6 and now I have a controller named modifyfiles. The index function of this controller accepts an argument.
Using the given argument, the index controller will search a desired data in a model. The following code is my index function :
public function index($id){
$_SESSION['logged_in']['idmod'] = $id;
$this->data['modullist'] = $this->__getmodulbyid($id);
$this->data['lecturelist'] = $this->__getlecturelist();
if(count($this->data['modullist']) <= 0){
show_404();
} else {
$this->load->view($this->layout, $this->data);
}
}
It works very well for now, when I try to insert wrong number arguments it directs me to the codeigniter built-in 404 page.
But when I try to insert some non numbers arguments, say some characters, it directs me to my own 404 page.
Here's my routing rules :
$route['404_override'] = 'home/pagemissing';
$route['dosen/modifymodul'] = 'dosen-tools/modifymodul';
$route['dosen/modifymodul/(:num)'] = 'dosen-tools/modifymodul/index/$1';
I want when users input wrong arguments, no matter they were numbers or characters, the function will redirect to my own 404 page, not the built-in one. How can I achieve that?
I'm pretty sure the problem is in my conditional block
if(count($this->data['modullist']) <= 0){
show_404();
} else {
$this->load->view($this->layout, $this->data);
}
What should I write to replace the show_404() ?
Or if I'm not wrong, how can I replace the show_404() with an 404_override trigger?
Solved
I did a little research and ended up creating this callback in one of my routing rules
$route['dosen/modifymodul/(:num)'] =
function ($id){
if(is_numeric($id)){
return 'dosen-tools/modifymodul/index/'.$id;
} else {
return 'home/pagemissing';
}
};
Thanks everybody

codeigniter 404 page not found trying to view one item

This is my first question on here so please be kind! I'm setting up a Codeigniter program for the first time, and while I have my index view working, I can't get the individual view page to work. It returns a 404 page not found error.
I have code in my foreach for the index view pointing to where I think the url should be:
<a href=<?= site_url("kittens/view/" . $item->id)?>>Details</a>
This takes me to localhost:8888/index.php/kittens/view/1 if I click on the kitten with an id of 1. But I get a 404 error.
Here is my controller function for the view:
function view(){
$this->load->model("kittensmodel");
$this->load->view('_header');
$this->load->view('detailview', $data);
$data['kitten_item']= $this->kittensmodel->details();
}
And here is the function in my model:
function details(){
$id = $this->url->segment(3);
$this->db->select('*')
->from('kittens')
->where('id', $id);
$data = $this->db->get();
return $data->result();
}
And my detailview file just has this, to test it:
<?php
print_r($kitten_item);
?>
I've been playing around with the routes but haven't had any luck with it. Here's what I have right now:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['kittens/(:any)'] = 'kittencontroller/view/$1';
I'm new to php and codeigniter and the answer might be really simple, but I would really appreciate the help!
try to use this link
<a href=<?= site_url("kittens/".$item->id)?>>Details</a>
try it
<a href=<?= site_url("kittens/view?q=" . $item->id)?>>Details</a>
in controller
function view(){
$q = $this->input->get('q',True);
$this->load->model("kittensmodel");
$data['kitten_item']= $this->kittensmodel->details($q);
$this->load->view('_header');
$this->load->view('detailview', $data);
}
in model
function details($q){
$this->db->select('*')
->from('kittens')
->where('id', $q);
$data = $this->db->get();
return $data->result();
}
and in detailview
<?php
print_r($kitten_item);
?>
I think it is because your site uri link should be base_url and
Details
<a href=<?= base_url("kittens" .'/'. $this->url->segment(3))?>>Details</a>
Routes:
$route['kittens/(:any)'] = 'folder/controller-name/$1';
$route['kittens/(:any)'] = 'controller-name/$1';
Or
$route['kittens/(:any)'] = 'folder/controller-name/index/$1';
$route['kittens/(:any)'] = 'controller-name/index/$1';
Always make sure your link matches your routes that you set.
If your Controller is actually called kittenscontroller, then your route should be
$route['kittens/(:any)'] = 'kittenscontroller/view/$1';
CI will generate a 404 Page Not Found if your controller does not exist.
I noticed you use plural kittensxxxx everywhere else except in your controller used in the above route.
You Should always check this sort of thing by manually entering the actual URL to test it.
IF you are going to use that route, then your Links should become
Details

Passing value through url

I am trying to send a url from view page to controller but it does not seem to work the way i am thinking.
View Page
Product
User
I want to get "tbl_product"
Controller admin
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->uri->segment(4);
}
}
?>
but if the segment(4) is changed to segment(3), it shows up with displaying "product" in the screen
your controller function should have arguments for your url segments
for example:
public function test($product = 'product', $tbl = 'tbl_product') {
echo $tbl // contains the string tbl_product
}
Since you said your routes look like this:
$route['default_controller'] = "admin";
$route['404_override'] = ''
and your URL is like this:
<?= base_url() ?>admin/test/product/tbl_product
Then if your base_url() is localhost/my_app, your URL will be read as this:
http://localhost/my_app/admin/test/product/tbl_product
http://localhost/my_app/CONTROLLER/METHOD/PARAMETER/PARAMETER
So in your controller, you can do this:
class Admin extends CI_Controller {
public function test($product = NULL, $tbl_product = NULL) {
echo $product;
echo $tbl_product;
}
}
It's strange to use codeigniter for this purpose, because codeigniter uses as default the url format bellow.
"[base_url]/[controller]/[method]"
I think it will be better and more easy to just pass the values you want as get parameters and make some httaccess rules to make your url more readable for the user and robots. That said you can do that:
Product
<?php
class Admin extends CI_Controller {
public function test() {
echo $this->input->get('product');
//should output 'tbl_product'
}
}
?>
If you prefer to use uri instead u should route your uri's so it will be like.
In your routes file you probably I'll need something like this.
$route['product/(:any)'] = "Admin/test";
This way you will probably access the uri segments correctly.
Thank you so much for going through.
$this->uri->segment(4); // is now working :S
its not working properly after all I made changes to routes.php and came back to default again. I seriously have no idea what the reason behind not displaying the result before.

Codeigniter routes for passing get parameter to index function

I have a url www.mywebsite.com/store/123456
where store is my controller class and I have a index function in it where Im getting the value after after store ,ie 123456.But im not able to achieve it.
As found online,I have tried adding this to routes $route['store/(:any)'] = 'store/index'; also tried
$route['store/(:any)'] = 'store/index/$1';
but doesn't seem to work.Please help me out.
In controller index function is
public function index()
{
echo $this->uri->segment(1);
}
But its not working.Pleae help
You are invoking 123456() method instead of index() method therefore you get CI's 404.
The simplest way is to use this kind of route
$route['store/(:any)'] = 'store/index/$1';
AND in top of it add parameter to index function in your case
public function index($parameter)
{
echo $this->uri->segment(2);
}
note that I changed segment parameter please see documentation.
using _remap()
function _remap($parameter){
$this->index($parameter);
}
function index($p)
{
echo $p; //shows 123456
}
If I remember correctly:
segment(n) gives you the segments of your uri before the routing takes place
rsegment(n) gives you the segments of your uri after routing (if routing occurred or the same as segment if it didn't)
so for /store/123456 getting rerouted to /store/index/123456
segment(1) == rsegment(1) == 'store'
rsegment(2) == 'index'
segment(2) == rsegment(3) == '123456'
Route:
$route['terms_and_conditions'] = 'SO_front/page/1';
controller :
public function page($id)
{
echo $id;
}
Output :
1
Here my solution for CI3...i would prefer laravel for advanced routing, orm,restapi, build in vuejs.
$route['store/(:any)'] = 'store/index/';
public function your index(){
$parameter=$this->uri->segment(2);
//use parameter for sql query.
}
Let say your route $route[store/product/update/(:any)] , then $parameter=$this->uri->segment(4)
Problem?. You will need to change entire file code if you plan to change the route name include view, controller, and custom route.

Categories