codeigniter route does not route as expected - php

in php word "list" is reserved so i had to use "listby" and create route.
According to CI User Guide I have created a route as follows:
$route['list'] = "listby";
It's routing perfectly the index function with url like "http://myserver.com/list" but does not route other functions i.e.. "http://myserver.com/list/uuid".
Here's contorller code:
class Listby extends CI_Controller
{
public function index()
{
echo "index";
}
public function userid()
{
echo "userid";
}
public function uuid()
{
echo "uuid";
}
}
Side note: with url like "http://myserver.com/listby/uuid" works fine.
Any clues where is the problem?

try:
$route['list/(:any)'] = "listby/$1";

Related

How can I define my function when using REST API in CodeIgniter?

I created a controller named Api.php then I extended the Rest_Controller. I noticed that I can only use index_get() when creating a function in this controller
<?php
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
}
public function index_get(){
$car_id = $this->get('car_id');
if(!$car_id){
$this->response("No Car ID specified", 400);
exit;
}
$result = $this->model_getvalues->getCars( $car_id );
if($result){
$this->response($result, 200);
exit;
}
else{
$this->response("Invalid Car ID", 404);
exit;
}
}
}
but when I try creating my desired function like getAllCars() instead of index_get() I get an error message telling me unknown function.
How can I define my own function instead of using index_get() when using rest api library in CodeIgniter?
Thanks i have been able to figure it out, i just found out that the name before the _get is what matters to the url i.e when one has a method like getCars_get, you will have to call it using just getCars without the _get attach to it, it work for me. it means that one can have more than _get method in the API controller.
By default on https://github.com/chriskacerguis/codeigniter-restserver#handling-requests the method is index_get(),another way to use your own method is play with the HTTP GET parameter
ex:
if($this->get('car_id') == 'all'){
//your own function here
}
Or if you really want to create your own method you can refer on this http://programmerblog.net/create-restful-web-services-in-codeigniter/
All you have to do is change function index_get() to getAllCars_get(),
`<?php
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
}
public function getAllCars_get(){
//your code
}
}
?>
`
Like this

Codeigniter Change URL method name

I am new in CI.
I want to change function name in addressbar url with add_car to addcar.
Actually my url is created as below
http://localhost/projectName/controller/add_car
But I want following in URL
http://localhost/projectName/controller/addcar
Is it possible? Please help me.
[Note] : My actual method name is add_car.
You can do it by two methods
Method 01
Edit - config/routes.php
$route['controller/addcar'] = 'controller/add_car';
$route['controller/deletecar'] = 'controller/delete_car';
output - www.exapmle.com/controller/addcar
Method 02
change your controller function name as you like.
public function addcar($value='')
{
# code...
}
public function deletecar($value='')
{
# code...
}
Output -www.exapmle.com/controller/addcar
Further Knowledge
If you use $route['addcar'] = 'controller/add_car'; URL looks like
www.exapmle.com/addcar
Change add_car function to addcar in your controller
function add_car(){
//...
}
To
function addcar(){
^
//...
}
Or in routes.php
$route['controller/add_car'] = "controller/addcar";
$route['controller/([a-z]+)_([a-z]+)'] = "controller/$1$2";
Above example will route every requested action containing '_' between two strings to action/method without the '_'.
More about Code Igniter regular expression routes:
https://ellislab.com/codeigniter/user-guide/general/routing.html
You can use this on your route:
$route['addcar'] = 'Add_car/index';
$route['addcar/(:any)'] = 'Add_car/car_lookup/$1';
and your controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Add_car extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function car_lookup($method = NULL)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index(); // call default index
}
}
public function index()
{
echo "index";
}
public function method_a()
{
echo "aaaaa";
}
public function method_b()
{
echo "bbbbb";
}
}

Error:404 page not found, code igniter

controller.php
<?php
define('_root',$_SERVER['DOCUMENT_ROOT']);
include(_root.'/innoshop/application/models/model.php');
// include_once 'model.php';
class Controller {
public $model;
public function __construct()
{
$this->model = new Model();
}
If i put localhost:8888/projectname, i got error like this
404 Page Not Found
The page you requested was not found.
anyone help me
As the guys say you should read the docs as this is very wrong. To fix do this..
class Controller extends CI_Controller{//better to give your controller a more meaningful name
public function __construct(){
parent::__construct();
//use the CI loader - the model will then be available like this $this->model->some_function();
$this->load->model('model');//better to give your model a more meaningful name as well
}
//the index method allows you to use just the controller name in your URI eg localhost:8888/projectname/index.php/controller
public function index(){
echo 'something to see';
}
//an alternative controller method get it like localhost:8888/projectname/index.php/controller/your_method_name
public function your_method_name(){
echo 'something to see in your method';
}
}
If you want rid of the index.php in the URI search for questions related to .htaccess in CodeIgniter
If you want to be able to use a uri like this localhost:8888/projectname then you need to add a route in config/routes.php that defines the default controller like this $route['default']='controller';

Unable to call multiple view from the same controller in CodeIgniter

I have created two views in CodeIgniter and I have created controller named HelloWorld.php
It contains two views.. but my problem is that the second view never gets called.
http://localhost/CodeIgniter/HelloWorld/Hello
Works fine for me, but second view
http://localhost/CodeIgniter/HelloWorld/Buzz
Doesn't call the second view
Here is my code
<?php
class HelloWorld extends CI_Controller
{
var $name;
var $color;
function __construct()
{
parent:: __construct();
$this->name= 'Suzzu';
$this->color = 'aqua';
}
public function Hello()
{
$this->load->view("hello");
}
public function Buzz()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view("welcome",$data);
}
}
what's the problem ??
Your code works as expected for me provided that:
there is a view file "views/welcome.php"
there is a view file "views/hello.php"
Can you verify that HelloWorld::Buzz() is being called?
public function Buzz()
{
die('yes, it works');
}
If this function doesn't execute when you go to /localhost/CodeIgniter/HelloWorld/Buzz, can you provide the following:
$config['base_url'] (config/config.php)
$config['index_page']
config/routes.php contents
change $config['base_url']="localhost/codeigniter/contollername"

How to pass parameters in index function in codeigniter

here's an example of url:
http://www.example.com/search/search-keyword
I am trying to make this work, I removed the index.php using .htaccess, search is the controller and I want to pass a parameter in the index method.
this is currently what it looks like:
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
any suggestions? I can't seem to make it work
You need the index segment http://www.example.com/search/index/search-keyword.
Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
Or you can look at remap
Remember not to trust user input, especially when you are throwing it into your url. The latest version of CI supports $_GET variables now, so you may want to look into using that or flashdata. A searh term as simple as O' Brien will give you a fatal error ("The URI you submitted has disallowed characters.").
class Search extends CI_Controller{
function _remap($param) {
$this->index($param);
}
function index($param){
echo $param;
}
}
You should then be able to access that as: /search/123123 :and the page would echo out "123123" or whatever you put in place of that.
function _remap($parameter){
$this->_index($parameter);
}
Give it a try and tell us how it went.
Most of these solutions will only work if you only have a single function called index() in your controller. If you have this in your routes:
$route['search/(:any)'] = 'search/index/$1';
With the above in your routes, what will happen is that your search function will work but then if you add any other functions to that same controller they'll all be redirected to /search/index/$1.
The only solution I can think of that allows you to use the URL you want while still being able to add any other functions to your search controller is to add a sort of messy conditional to your routes file. Here's what it will look like and what it does:
$request = $_SERVER['REQUEST_URI']; // Only add this for readability
if(!strpos($request, 'search/another_function') || !strpos($request, 'search/more_functions')) {
$route['search/(:any)'] = 'search/index/$1';
}
What this is doing is basically saying "if the requested URL doesn't contain the name of any of my search controller functions then it must be meant for the index so we'll activate the route rule for index".
This will allow you to take requests for search/any_other_functions_you_have without issue and only activate the rule for hiding the index function when a request URI doesn't match any of them.
One side effect of this is that you'll never get a 404. For example, if someone enters a URL like "yourdomain.com/search/something" and they expect it to show a non-search result page they won't get a 404 alerting them to the fact that there is no page like that and instead the app will assume what they typed is a search term. However, it sounds like this isn't much of an issue for you and I don't see it being such a terrible thing for one controller to be unabe to return 404s.
You need to understand the way code igniter urls work, its basically like this:
http://www.mysite.com/{controller}/{function}
So what your url is actually looking a function called "keyword" in your search controller.
You have multiple options. The easiest will be to simply change the url to:
http://www.mysite.com/search/result/keyword
Then this should work perfectly:
class Search extends CI_Controller{
function result($param){
echo $param;
}
}
If you really want to use the url as you had it, you can use the same snippet of code as above but also open up "application/config/routes.php" and add this to the bottom.
$route['search/(:any)'] = "search/result";
Alternatively if you want to continue using the index function you can change it to this
$route['search/(:any)'] = "search/index";
And change your class to something like this:
class Search extends CI_Controller{
function index($param=FALSE){
if($param == FALSE) {
//Show search box
} else {
//Show search results
}
}
}
Don't forget to update your answer if you figure it out yourself or accept somebodies answer if it answers it :)
I've just started CI and here was my solution and implemented it on my website. So far it works for me and my search queries have been indexed by google as links
Class Search extends CI_Controller{
function index(){
$search_item = $this->input->post('search_box'); // this is from the input field
redirect(base_url()."search/q/".url_title($search_item));
}
// i've redirected it to the "search" controller then :
function q($search_item = null){// process search
// load the view here with the data
}
}
This is my solution:
From CI userGuide
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
So I've created my controller in this way:
class Shortener extends CI_Controller {
function shortener() {
parent::__construct();
}
function index($getVar) {
echo "Get var: " . $getVar;
}
function config() {
echo "another function";
}
function _remap($method) {
if($method == "config") {
$this->$method();
}
else {
$this->index($method);
}
}
}
Hope this can be helpful to someone...
BUG: if you call /shortener _remap function pass "index" to index() function...it can be solved adding an if condition or something else
You should use the CI URI Class. http://codeigniter.com/user_guide/libraries/uri.html
class Search extends CI_Controller{
function index($param){
//echo the search-keyword
echo $this->uri->segment(2);
}
}
This easiest way is just to use uri segments:
class Search extends CI_Controller{
function index(){
$param=$this->uri->segment(2);
echo $param;
}
}
Or like Madmartigan said, use routes, which is probably the better way of doing this.
I had to also check if some variable was passed or not - so here is the solution that I took.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Story extends CI_Controller{
function story() {
parent::__construct();
}
function _remap($parameter){
$this->_index($parameter);
}
public function _index($story_id) {
if($story_id == 'index'){
//No variable passed
redirect('auth', 'refresh');
}else{
$data['title'] = "Story";
$this->load->view('story', $data);
}
}
}
Hope this helps someone!
Thank you. This code works for me. If parameter is a number
Or you need to use a route $route['search/(:any)'] = 'search/index/$1';
However, If the parameter is a string (http://[::1]/note/getnote/index/fg => http://[::1]/note/getnote/fg). I use _remap() like this code.
<?php
class Getnote extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('image_lib');
}
public function index(){
}
public function _remap($keyname)
{
$this->load->model('Laydb');
$data = $this->Laydb->getNote($keyname);
$proListArray['patitle'] = "Edit notepad";
$this->load->view('top', $proListArray);
$this->load->view('editblog', $data);
$this->load->view('bottom');
}
}
class Search extends CI_Controller{
function index($param){
echo $param;
}
}
In routes.php
$routes['search/(:any)'] = 'search/index/$1';
It's all
I finally found a workaround since I cant simply put post variables into the URL.
What I did was create another function, then redirect it to that function.
class Search extends CI_Controller{
function index(){
$search_item = $this->input-post('search_item');
redirect("search/q/".url_title($search_item));
}
function q($key){
// process search
}
}

Categories