How to remove index segment in my codeigniter url - codeigniter

I am using rest controller api by phil sturgeon this is the url
paycent/user/index/id/1. Thanks in advance!

you need to set route for that.
set route in your application/config/routes.php file
$route['paycent/user/id/(:any)'] = 'paycent/user/index/id/$1';
Note : If paycent is your project name than remove it from your route. and for creating link you just need to write <?php echo site_url('paycent/user/id/1'); ?>. It will redirect to indexmethod.

In application/config/config.php :
Change
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';

Related

Routing issues with Codeigniter project

In my routes.php file I have the following codes:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = function ($val){
require_once( BASEPATH .'database/DB.php' );
$db =& DB();
$db->select('url');
$db->from('interior_form');
$db->where('url',$val);
$query = $db->get()->row();
$db->close();
if(sizeof($query)>0):
return 'home';
else:
return "404_override";
endif;
The issue that I am facing here is whenever I put www.xyz.com/admin/login , it goes to the home page first then again if I write www.xyz.com/admin/login in the same browser, only then it goes to the admin login page.It does not go to the admin login page on the very first instance.
Try:
$route['admin/login'] = 'admin/login/index';
$route['admin/add_client'] = 'admin/add_client/index';
$route['(:any)'] = 'home/$1';
In your home controller, there you handle which request is 404 and which is valid.
You don't need to edit your Rout file.first of all come out from that file.
and you can do this.....
In admin controller page create a function called index.in that function you load your login page.
whenever you type www.xyz.com/admin then load the admin login page.
note:-
Which controller page contains index function,first that index function is load when call that controller, because of index function is first priority compare to other functions in all controllers page.
You may try this simple code for set up rout as bellow.
$route['default_controller'] = 'Adminlogin';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Codeigniter: error in base_url

i have one problem with function base_url() in my codeigniter.
I'm configure the condeigniter to:
$config['base_url'] = 'sistema_clientes/';
$config['index_page'] = '';
and autoload to:
$autoload['helper'] = array('url');
It's ok, but when i'm try to use base_url('clientes') in my pages, the link repeat.
example, i wanna this : localhost/sistema_clientes/example.
<li>Example</li>
in my url, the link go to: http://localhost/sistema_clientes/sistema_clientes/clientes
but i wanna this: http://localhost/sistema_clientes/clientes
i don't wanna the base_url() repeat the sistema_clientes, please help me.
what i can do to repear this?
Thanks.
Set your base_url like below:
$config['base_url'] = 'http://localhost/sistema_clientes/';
Because setting $config['base_url'] = 'sistema_clientes/'; makes sense that
you are going to sistema_clientes controller so base_url
becomeshttp://localhost/sistema_clientes/sistema_clientes.

Code-igniter href duplicate base url

<li>Home</li>
that give to me localhost/farmex/localhost/farmex/home
and suppose to be localhost/farmex/home
in routes.php you can set $route['default_controller'] = 'home'; and change your code to
<li>Home</li>
Hope this help! ^^
base_url need to set to get proper link from site_url function.
At your application/config.php you need to set
$config['base_url'] = 'http:/localhost/farmex/';
$config['index_page'] = '';
Remember $config['index_page'] is also important.
If you set
$config['index_page'] = 'index.php';
site_url('home') will produce http:/localhost/farmex/index.php/home
$config['index_page'] = 'index.php?'; will produce http:/localhost/farmex/index.php?/home which is needed sometimes.like if you don't want to use .htaccess file.

Why Codeigniter3.0's url can't be blank after doamin name?

I'm corrently develop webs application for selling products online by using Codeigniter and upgrade from Ci2.2.1 to CI3.0.
However I've meet an errors 404 Page Not Found when I enter domain name on url not yet called any controller by hoped default_controller will load instead of type my main controller.
Notes: It is working on Ci2.2.1 and it stop work when upgrade to CI3.0.
It is working if I type any characters or any Controller after domain name(http://localhost/Ecom3/sometroller) But it is coudn't call default_controller and keep empty or blank after domain (http://localhost/Ecom3/).
Errors log: ERROR - 2015-04-24 03:48:57 --> 404 Page Not Found: /index
Please check my webs structure below:
Main->Main_controller->MY_controller->CI_Controller
My purpose to make it easy to controller the templates.
In MY_controller I've use __autoload() function
function __autoload($class){
if (strpos($class, 'CI_') !== 0)
{
if (file_exists($file = APPPATH . 'libraries/' . $class .'.php'))
{
include_once $file;
}
}
}
And I have confige route as below
$route['default_controller'] = "main/Main";
$route['(:any)'] = 'main/main/index/$1';
$route['c'] = 'cat/cat/index/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
The problem:
My url can't be blank after I type domain or after forword slash as below image
Please help
I try your code and get 404 when Main.php in your controller place inside controller/main folder. but when im move Main.php into controller folder, default controller to Main has working. then im try to debuging and see:
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2)
{
$method = 'index';
}
in core/Router.php
thats mean codeigniter3 are not allowed you to set default_controller in sub folder. you must put default_controller in app/controller/ to make it works.
in config/router.php
change this:
$route['default_controller'] = "main/Main";
TO:
$route['default_controller'] = "Main";
then move your controller file :
app/controller/main/Main.php
TO
app/controller/Main.php

Change Codeigniter url

I'm using Codeigniter.I want to set href attr to something like :
<a href="/contact.html" >Contact</a>
But i get 404 error because i should write
Contact.
Where is some thing to fix this problem.
Any help please.
Assuming that you have a controller by the name Contact and you successfully extend the CI_Controller class, go to application/config folder and in config.php find:
$config['base_url'] = 'http://www.youdomain.com/';
Then in your internal links you should do:
Contact
If you are using javascript to make the redirect, put on top of the js file:
var host = 'http://www.yourdomain.com/';
Again:
window.location.href = host + 'contact';
If you're using codeigniter, you do not want to point to an .html file.
If you're using codeigniter correctly, you should use the helper methods that exist in codeigniter.
Instead of writing the anchor tag yourself, try this:
<?php echo anchor('contact', 'Contact'); ?>
to add the suffix to your controller in calling go to config/config.php and search for
$config['url_suffix'] = '';
and assign html to it to become
$config['url_suffix'] = 'html';

Resources