CodeIgniter URI Routing and empty segment - php

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

Related

$this->uri->segment(2) giving me 404 error when in Index function in controller

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";

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.

SEO url gives 404-error in CodeIgniter

I am pretty new to codeigniter. I do know php.
How can I accomplish to load the right view?
My url: /blog/this-is-my-title
I’ve told the controller something like
if end($this->uri->segment_array()) does exist in DB then load this data into some view.
I am getting an 404-error everytime I access /blog/whatever
What am i seeing wrong?
unless you're using routing, the url /blog/this-is-my-title will always 404 because CI is looking for a method called this-is-my-title, which of course doesn't exist.
A quick fix is to put your post display code in to another function and edit the URLs to access posts from say: /blog/view/the-post-title
A route like:
$route['blog/(:any)'] = "blog/view/$1";
may also achieve what you want, if you want the URI to stay as just `/blog/this-is-my-title'
The may be more possibilities:
The most common - mod_rewrite is not active
.htaccess is not configured correctly (if u didn't edited it try /blog/index.php/whatever)
The controller does not exist or is placed in the wrong folder
Suggestion: if you only need to change data use another view in the same controller
if (something)
{
$this->load->view('whatever');
}
else
{
$this->load->view('somethingelse');
}
If none of those works post a sample of code and configuration of .htaccess and I'll take a look.
The best way to solve this problem is to remap the controller. That way, you can still use the same controller to do other things too.
No routing required!
enter code here
<?php
class Blog extends Controller {
function __construct()
{
parent::__construct();
}
public function _remap($method, $params = array())
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->show_post();
}
}
function index()
{
// show blog front page
echo 'blog';
}
function edit()
{
// edit blog entry
}
function category()
{
// list entries for this category
}
function show_post()
{
$url_title = $this->uri->segment(2);
// get the post by the url_title
if(NO RESULTS)
{
show_404();
}
else
{
// show post
}
}
}
?>

Categories