CI Routing for "#" and "?" - php

I am working on Codeingiter and have a Function in one of my class.
class Search extends My_Controller{
public function index(){
//my search code here...
}
}
Now for this function i need set a route so i can make the URL look like
http://example.com/s/?q=searchtext
or
http://example.com/s/#q=searchtext
However when i do this :
$route['s/?q=(:any)'] = "search/index";
$route['s/#q=(:any)'] = "search/index";
It does not work and i am being redirected to 404 page.
It seems CI does not allow this kind of Routing. Please tell me how i can do this.
Also If i use http://example.com/s/#q=searchtext
how can i get the value searchtext in my function ?

Related

Codeigniter routing the proper way

I want to understand how codeigniter routing works.
i know that codeigniter interprets url as www.example.com/class/function/id. I have defined this url below but it returns me to the page where I am currently at. say am at about.php page , it returns me to the same page
$route['webadmin/teacher/class/subject/(:num)/(:num)'] = "admin/teacher_details/$1/$2";
in my admin controller i have defined teacher_details as
public function teacher_details($a='', $b = '', $c ''){}
what i want is for the defined url to render as www.example.com/webadmin/teacher/class/subject/id1/id2 for the controller admin/teacher_details
codeigniter support segment based url, not query like
in we pass url as domain/controller/funtion_name/parameter.
suppose you have created a controller named demo with function demo_function
class Demo extends CI_Controller
{
public function demo_function($id='')
{
echo "demo"
}
}
then you will create route in config/route.php
$route['demo/(:num)'] = 'demo/demo_function';
you can access parameter of url like $this->url->segment(3)
or $this->input->get('parament_name');

How to add a view?

I've been researching up on how to add views and I'm stumped. I want to add a view and all I get are 404 errors. All the examples I see on the web are just to add a default controller. I have a default controller, now I want to add a new page passed an ID in the URL.
This is the controller xyz.php:
class Xyz extends CI_Controller {
public function index() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_main_view');
}
public function activity() {
date_default_timezone_set('UTC');
$this->load->model('xyz_model');
$this->load->view('xyz_activity_view');
}
}
Model is xyz_model.php, and views are xyz_main_view.php and xyz_activity_view.php.
This is routes.php:
$route['default_controller'] = 'xyz'; // works okay
// $route['xyz'] = 'xyz/activity'; // 404
// $route['activity'] = 'xyz/activity'; // 404
// $route['xyz/activity'] = 'xyz/activity'; // 404
// ... many, many other different approaches
I'm able to use http://localhost, but I'd like to use the following:
// map to main view
http://localhost/index
http://localhost/xyz
http://localhost/xyz/index
// map to activity view
http://localhost/activity
http://localhost/xyz/activity
My understanding is that some of the URLs for the main view should work automatically, not seeing it. Just http://localhost.
I haven't even touched how to get an ID from the URL for the activity page. Just want to get over this first hurdle.
Keep this code in routes
$route['default_controller'] = 'xyz';
Then Try this URL to execute "activity()" function in xyz controller.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity
To Pass Parameters such as id's you can use this url.
http://localhost/[YOUR PROJECT FOLDER NAME]/index.php/xyz/activity/[ID]
For more information please check codeigniter routing library. It is really easy to understand.
https://www.codeigniter.com/user_guide/general/routing.html

Writing rules in routes.php for Codeigniter

I need a help for routes.php
I have 2 types of URLs like -
https://www.seekmi.com/service/jakarta/digital-marketing
and
https://www.seekmi.com/en/service/jakarta/digital-marketing
and for those i wrote 2 rules in routes.php with same controller as -
$route['en/service/(:any)/(:any)'] = "findservice/search/$1/$2";
$route['service/(:any)/(:any)'] = "findservice/search/$1/$2";
but only first URL works, not second one.
Can any one of you please help me resolve this issue ?
try this
$route['service/(:any)/(:any)'] = "findservice/search/$1/$2";
$route['en/service/(:any)/(:any)'] = "findservice/search/$1/$2";
The links are okay but the main aspect of the route mapping is the Controller and function name you specified in the setting. If they do not exist you will get 404 error.
So you need to create a findservice Controller and a method search in the controller to accept two parameters.
//save as findservice.php in application/controller/ folder
class Findservice extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function search($param1,$param2){
//use $param1 and $param2
}
.....
}

How to route cutom URL with to custom controller in CodeIgniter?

I have a PHP CodeIgniter Controller with name User and have a method that get details of user user_detail($username)
Now when i need to show user data for example for userName mike
I call this URL
http://www.example.com/user/user_detail/mike
My target
How to make user data accessible by next URLs
http://www.example.com/user/mike
or / and
http://www.example.com/mike
You have to read the this page from the official documentation of codeigniter. It covers all related things to Routing URLs easily. All routes must be configured via the file:
application/config/routes.php
It could be something like this :
$route['user/(:any)'] = "user/user_detail/$1";
This can be achieved by overriding CI_Controller class BUT dont change the original core files, like I said override the controller and put your logic in it.
Help: https://ellislab.com/codeigniter/user-guide/general/core_classes.html
how to create Codeigniter route that doesn't override the other controller routes?
Perhaps an easier solution would be to route it with the help of apache mod_rewrite in .htaccess
Here is an detailed explanation on how to achieve it: http://www.web-and-development.com/codeigniter-remove-index-php-minimize-url/
Hatem's answer (using the route config) is easier and cleaner, but pointing the usage of the _remap() function maybe helpful in some cases:
inside the CI_Controller, the _remap() function will be executed on each call to the controller to decide which method to use. and there you can check if the method exist, or use some defined method. in your case:
application/controllers/User.php
class User extends CI_Controller {
public function _remap($method, $params = array())
{
if (method_exists(__CLASS__, $method)) {
$this->$method($params);
} else {
array_unshift($params, $method);
$this->user_detail($params);
}
}
public function user_detail($params) {
$username = $params[0];
echo 'username: ' . $username;
}
public function another_func() {
echo "another function body!";
}
}
this will result:
http://www.example.com/user/user_detail/john => 'username: john'
http://www.example.com/user/mike ........... => 'username: mike'
http://www.example.com/user/another_func ... => 'another function body!'
but it's not going to work with: http://www.example.com/mike , since the controller -even if it's the default controller- is not called at all, in this case, CI default behaviour is to look for a controller called mike and if it's not found it will throws 404 error.
for more:
Codeigniter userguide 3: Controllers: Remapping Method Calls
Redirect to default method if CodeIgniter method doesn't exists.

Codeigniter 2.0 GET params with URI rounting

Hey,
I've wrote a line in my routes.php as the following:
$route['admin/trip/add'] = "admin/trip_controller/form";
But when I go to that URL in my browser, I get sent back to the main index page i.e (www.mydomain.com), does anybody know what i'm doing wrong?
I've enabled GET params in my config file too:
$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = TRUE;
I've also tried going to a URL which doesnt use routing and I too get redirected back to the main index page.
Thanks
enable_query_strings is a configuration option that will allow you to send your controller and method via ?c=blog&m=view. This will cause trouble because obviously you aren't sending those in your query string, so CodeIgniter will assume nothing is passed and display the homepage.
You should try to use the _remap() function instead of messing with the routes.
So I guess you would have the admin.php controller.
Inside you would use the remap function which will use the second segment of the url to find what function to call.
<?php
class Admin extends Controller{
function _remap($method, $params =array())
{
if(method_exists($method))
{
$this->$method();
}
elseif($method == 'trip' && $this->uri->segment(3)=='add')
{
//do what you want here
}
}

Categories