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
Related
I'm creating a users profile section for my website, and the URL would be /users/handle/
for some reason when using $this->uri->segment(2) it is just sending me to my 404 error page.
The weird thing is, if i change the function to profiles and then make it segment(3) and go to the url /users/profile/handle the code works fine and loads up the right view.
I've tried changing the segment number to 0,1,2 & 3 and neither seem to work when in my users Index function.
Here is the function for my index below.
public function index() {
$username = trim(strip_tags($this->uri->segment(2)));
if (!$username) {
$data['error'] = _('User not found');
$this->load->view('user-profiles', $data);
} else {
$profile = $this->db->get_where("users", ["handle" => $username]);
$profile = $profile->row();
$data['profile'] = $profile;
$this->load->view('user-profiles', $data);
}
}
Any help would be greatly appreciated! thanks
I seemed to of worked it out.
For anyone else who runs into a similar problem, i edited my routes.php file and added
$route['users/(:any)'] = "users/index/$1";
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.
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.
I now set uri routing in my codeigniter config like this
$route['user/:any'] = "users";
and everything works fine with request like this
http://localhost/user/1
but if i try request
http://localhost/user/
so uri segment is empty, i am getting error
404 Page Not Found
The page you requested was not found.
how to avoid this? I ve tried to use
$user = $this->uri->segment(2);
if (!isset($user)) redirect('/', 'refresh');
but this is not working.
You can add another route such as:
$route['user'] = "users"
build your functions in your controller instead of this:
public function index()
{
...
try something like this:
public function index($slug = '')
{
if ($slug = '') {
// redirect code here
}
else {
// if there is something in $slug then load this code
}
and maybe your routes should be something like:
$route['user'] = 'users';
$route['user/(:any)'] = 'users/index/$1';
Try with
$route['user:any'] = "users";
I think you need to fix the syntax, please try:
$route['user/(:any)'] = "users";
You need parentheses around the wildcard (:any).
Reference: http://ellislab.com/codeigniter/user-guide/general/routing.html see Examples
I am having problems with CodeIgniter pagination module.
When I go to click on the links I get a 404 error. As I understand and I may be incorrect, but it would seem that the URI segment which determines the offset is not being read inside the index function of my controller and that code igniter is actually looking for this as a function inside the controller hence throwing the 404 when it cant find it.
I have googled around and checked on the forum to find a solution but have not had any success. If anyone has any hints or tips that would be great I have included the relevant code from my controller, model and view below.
//Controller
$this->load->library('pagination');
$config['base_url'] = '/yourhistory/';
$config['total_rows'] = $this->db->get('gallery')->num_rows();
$config['per_page'] = 3;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data['galleryitems'] = $this->pages_model->get_gallery($config['per_page'], $config['uri_segment']);
$this->load->view('/gallery', $data);
//Model
function get_gallery($limit, $segment)
{
$query = $this->db->get('gallery', $limit, $this->uri->segment($segment));
if ($query->num_rows > 0)
{
foreach($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
//View
echo $this->pagination->create_links();
Well its going to fail first off because of your base_url:
$config['base_url'] = 'http://example.com/index.php/test/page/';
You need the full path, altho I am just guessing here as you don't provide the URL that is throwing the 404.
Notes on pagination here: http://codeigniter.com/user_guide/libraries/pagination.html
Your base_url is not correct:
//should be something like this
$config['base_url'] = base_url().'/controllername/functionname/';
See: http://codeigniter.com/user_guide/libraries/pagination.html