Change Codeigniter url - codeigniter

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

Related

How to display correct link in CI anchor

I have a form with upload file option.
I get the file_path on $data uploaded by user. Now I want to use this
file_path to be the link in my anchor tag.
My problem is i can't get the correct link. it seems like that the base URL was being included in the link showed in the lower left of the screen.
I also read some post here to add "http://" before the link. but the link appeared was like this
c/xampp/htdocs/.../uploads/products/filename.pdf
no colon.
any idea on how can I access my uploaded file be access via link.
Thanks
paste this line in your /applications/config/config.php just right before
$config['base_url'] = "";
and change this to
$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://');
$root = $protocol.$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;
This approach makes your base_url() function returns the current host or url you are in. Even if you upload this to any server it will always return the exact url of your website.
Then you use it like this
<?= base_url('uploads/products/filename.pdf'); ?>
That's all you have to do. Hope this helps!

How to remove index segment in my codeigniter url

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'] = '';

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

Add parameter to Codeigniter URL

I have a problem when i try to add other parameter to URL.
before i use Codeigniter i add those parameters using JavaScript like this
test
but when i tried to do it with Codeigniter i don't know how.
<?php echo anchor("home/index/param1","test"); ?>
as i said i want to add this parameter for example my URL looks like this
home/index/param2
so when i click on test i want the URL to be like this
home/index/param2/param1
Take a look at CodeIgniter's URL Helper Documentation
The first parameter can contain any segments you wish appended to the URL. As with the site_url() function above, segments can be a string or an array.
For your example, you could try:
<?php
$base_url = 'home/index/';
$param1 = 'param1';
$param2 = 'param2';
$segments = array($base_url, $param1, $param2);
echo anchor($segments,"test");
?>
You can't do that with the form helper, you have to use your js function again :
echo anchor("home/index/param2", "test", array("onClick" => "javascript:addParam(window.location.href, 'display', 'param1');"));
It will produce :
test
But I don't see the point of dynamically change the href on the click event. Why don't you set it directly at the beginning ?
echo anchor("home/index/param2/param1", "test");

Autoload Config for Pagination in CodeIgniter not working

I am trying to implement pagination in my CI webapp. Now I put the config for pagination inside a config file like this...
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = "http://example.com/index.php/home/index";
$config['num_links'] = "9";
$config['per_page'] = "20";
$config['total_rows'] = "200";
/* End of file pagination.php */
/* Location: ./system/application/config/pagination.php */
In my controller, I have loaded the library
$this->load->library("pagination");
And I have defined the pagination config file to be autoload in config/autoload.php
$autoload['config'] = array('pagination');
At last I called the method to create links in my view template:
<?php echo $this->pagination->create_links(); ?>
This did not create any links. The configuration is being autoloaded correctly. I checked using...
<?php echo $this->config->item("num_links"); ?> <!-- this dislayed 9 -->
What am I missing here? Just for the record, putting the config inside the controller didn't work either.
Update #1- I have found out that the config settings are loading correctly but they are not reaching the library or something like that. Inside the pagination library - I did some hard coding to find out that per_page parameter was zero in there.
Update #2- I was mistaken when I said that putting the config inline wasn't working. It is working fine. The autoload isn't working.
Regards
Finally used this code to solve my problem...
$this->config->load("pagination");
$page_limit = $this->config->item("per_page");
$config['total_rows'] = $var; // Some variable count
$this->pagination->initialize($config);
This lets me define the config items in a file as well as initialize the items I want in controller like in my case, the total no. of rows - retrieved from database.
Regards
Your autoload line in your config file should be this
$autoload['libraries'] = array('pagination');
And you must have this line in you controller after your config array, before you use create_links() etc.
$this->pagination->initialize($config);

Resources