I am working on a site in CodeIgniter.
I want my site users to select the city from a drop-down and then the content to be displayed on the basis of that city. For this I have two issues.
1) How can I add a new parameter to the URL segment. I checked this . Is it ok to create city as controller? but then how should I create the current controllers? If so,
2) Problem is I have already worked on all controllers.
Guide me on how should I proceed.
You could pass the uri segments as parameters to your controller.
http://YOUR_URL.com/index.php/city/get/zurich
<?php
class City extends CI_Controller {
public function get($city)
{
echo $city;
}
}
http://www.codeigniter.com/user_guide/general/controllers.html#passing-uri-segments-to-your-methods
Edit
Just to give you an idea:
First remove the index.php from the URL:
create the .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# When CI is in a subfolder use this line instead
#RewriteRule ^(.*)$ /ci/index.php/$1 [L,QSA]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open the file /application/config/config.php and search for the line
$config['index_page'] = 'index.php';
and change it to
$config['index_page'] = '';
Open the file /application/config/routes.php and add this line to the other rules
$route['(:any)/(:any)/(:any)'] = "$2/$3/$1";
And the controller looks like this.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class City extends CI_Controller {
public function index($city)
{
echo $city;
}
}
So I'm just changing the order of the segments. 2 = Class, 3 = Method, 1 = parameters.
try this.
-first tell me how u send request after selecting a city from dropdown.
-anyways i will tell you ,first add jquery 'change' event to dropdown list and on change you have to get the current value of the dropdown list as
e.g
$('#idOfDropDownlist').on('change',function(){
var value = $(this).val();
//now send a ajax request to controller to get information about city.
$.ajax({
type:'get',
url:"<?php echo base_url(); ?>ControllerName/methodName/"+value,
success:function(response){
console.log(response);
}
});
});
//---- your controller's method for getting this request will be like.
class ControllerName .....{
public function methodName($city = ''){
//--- here you got the city name,so do whatever u want with......
}
}
Related
My site is created in three languages https://anto-nguyen.com.
It works well to translate from one language to another one.
But.. I can't put the language indication into the URL.
I looked through all questions and answers that I could find here and have tried to use them, but unfortunatelly it doesn't work..
I use Controller LangSwitch
class LangSwitch extends CI_Controller {
public function __construct() {
parent::__construct();
}
function switchLang($language = "") {
$this->session->set_userdata('site_lang', $language);
redirect($_SERVER['HTTP_REFERER']);
}
}
The language is called by following code integrated into menu
<div>
<?= anchor(base_url("langSwitch/switchLang/english"), 'En'); ?>
<?= anchor(base_url("langSwitch/switchLang/french"), 'Fr'); ?>
<?= anchor(base_url("langSwitch/switchLang/russian"), 'Ру'); ?>
</div>
My .htaccess lookes like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
And seems to be modified, but all suggestions I found doesn't work
Could you help me what to do that my link will appeared as /mysite/fr /mysite/en /mysite/ru?
All translation are in the folders language/english language/french language/russian
Mayby to change something in routes?
$route['default_controller'] = 'site';
$route['work'] = 'work/index'; //the URL 'work' will redirect to 'work/index'
$route['work/(:any)_(:num)'] = 'work/article/$2';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['(:any)'] = 'site/$1';
I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.
The steps that I took to setup my new Blog controller involved:
Creating an A record pointing to my server's ip address.
Adding new rules to CodeIgniter's routes.php file.
Here is what I came up with:
switch ($_SERVER['HTTP_HOST']) {
case 'blog.notedu.mp':
$route['default_controller'] = "blog";
$route['latest'] = "blog/latest";
break;
default:
$route['default_controller'] = "main";
break;
}
This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.
Now here is the problem...
Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...
My .htaccess file looks like this (the default for removing index.php from the url):
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
And my Blog controller contains the following code:
class Blog extends CI_Controller {
public function _remap($method){
echo "_remap function called.\n";
echo "The method called was: ".$method;
}
public function index()
{
$this->load->helper('url');
$this->load->helper('../../global/helpers/base');
$this->load->view('blog');
}
public function latest(){
echo "latest working";
}
}
What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(
After 4 days of trial and error, I've finally fixed this issue!
Turns out it was a .htaccess problem and the following rules fixed it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks to everyone that read or answered this question.
Does blog.domain.co/blog/latest also show a 404?
maybe you could also take a look at the _remap() function for your default controller.
http://ellislab.com/codeigniter/user-guide/general/controllers.html#default
Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.
Straight from the user guide,
If your controller contains a function named _remap(), it will always
get called regardless of what your URI contains. It overrides the
normal behavior in which the URI determines which function is called,
allowing you to define your own function routing rules.
public function _remap($method)
{
if ($method == 'some_method')
{
$this->$method();
}
else
{
$this->default_method();
}
}
Hope this helps.
have a "AllowOverride All" in the configuration file of the subdomain in apache?
without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no
$route['latest'] = "index";
means that the URL http://blog.example.com/latest will look for an index() method in an index controller.
You want
$route['latest'] = "blog/latest";
Codeigniter user guide has a clear explanation about routes here
How to rewrite particular url of website using .htaccess or CI routing
http://www.domain.com/user_controller/newfunction/username
as
http://www.domain.com/username
$route['/(:any)'] = "/user_controller/newfunction/$1";
or try htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]
/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";
Its easier to use CI routing system found at application/config/routes.php
$route['(:any)'] = 'user_controller/newfunction/$1';
Don't forget to set your base url at application/config/config.php
$config['base_url'] = 'http://www.domain.com/';
Add remember to remove the index.php in the url. Edit your .htaccess file at the root of your site
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
now in your user_controller, the function newfunction will look like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
when you visit http://www.domain.com/nash, your code should print out nash
This is my first CI project so forgive me.
I simply can't hit a method with an AJAX call. It keeps coming up as a 404 in Web Inspector.
I have a controller called "home.php". It's working. I can land on my home page.
Then I have this AJAX call firing on a hover event
function showDataWindow(){
// i might switch this to data attr, but for now item IDs are contained in class
var thisClass = $(this).attr('class');
var thisIDpos = thisClass.indexOf("id-")+3;
var thisID = thisClass.substr(thisIDpos, 3);
alert(thisID); // alerting correctly
$.post('getMoreInfo', { ID: thisID},
function(data) {
.. act on data
I simply can find the method I am calling - getMoreInfo. Always 404.
I have a home.php class in my controllers and its set as my default, and it works because I am landing on my home page and getting the index. But in that home controller is also my getMoreInfo function...
public function getMoreInfo()
{
$ID = $_POST['ID'];
$this->load->model('Artist_model');
$assocReturned = $this->Artist_model->get_more_info($ID);
echo json_encode($assocReturned);
}
And I feel like there is a tiny MC Hammer guarding that function. "You can't touch this". He mocks me in his little parachute pants and minuscule fade.
I think it must be how I am doing my URI in the Jquery AJAX post? I have index re-writing in my htaccess (which I am kind of foggy on exactly)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
But I have tried just about every URI permutation in that AJAX call
www.mySite.com/index.php/home/getMoreInfo
index.php/home/getMoreInfo
index.php/getMoreInfo
/home/getMoreInfo
home/getMoreInfo
/getMoreInfo
getMoreInfo
!
And none have worked.
What you have to call depends on how your router is configured.
Default call would be /home/getMoreInfo, but could be changed if you have reconfigured your router. Reference: http://codeigniter.com/user_guide/general/routing.html
I am building a site entirely on codeigniter .I have set my default controller as cuff.
so whenever users type the domain name it takes the control to that controller.
class Cuff extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
$this->load->view('index');
}
public function navigate()
{
echo "test";
exit;
}
}
In my index view I want an anchor to navigate to a function in my default controller .
so when I write
our collection , it says page not found .
I have even set the base url like this
$config['base_url'] = "http://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= preg_replace('#/+$#','',dirname($_SERVER['SCRIPT_NAME'])).'/';
Cant seem to figure out the issue.
You missed the controller name
our collection
Set $config['base_url'] ='';
In your .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|favicon|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Make sure your controller is in controller's folder (not any subfolder) and is properly named cuff.php
Try navigating to http://yoursite/cuff/navigate
If you still see errors try rewriting the last line in .htaccess to:
RewriteRule ^(.*)$ /index.php/$1 [L]
(without question mark).
Please tell if that helps!
EDIT: and your links better all be in this form:
Link
The problem is in your routing.
Edit your routes.php (located in the folder ../application/config/) and add the code below:
$route['navigate'] = "Cuff/navigate";
For more information regarding Codeingiter URI Routing:
http://codeigniter.com/user_guide/general/routing.html
Hope this helped you.
Simply you use
"<?php echo site_url();?>/cuff/navigate">our collection</a>