I'm trying to retrieve string as parameter using following URL scheme:
www.myapp.com/[String]
Is there any way I can do this?
*Based on my research, Codeigniter doesn't accept string parameters unless I include the Controller's name in URL: www.myapp.com/[Controller Name]/[String]
But this doesn't solve my problem :(
You're right, CI requires controller name at URI, but You can use default_controller.
At config/routes.php add route rule $routes['(:any)'] = 'welcome/index';, remove index.php from Your URL (there're many tutorials and how-to for this), and at last useuriclass at Yourindex()method ofwelcome` controller:
function index(){
var_dump($this->uri->uri_string());
}
Related
I need to use 2 controllers in CodeIgniter 3. I have Welcome and Paypal controllers. In routing, previously I had following code:
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Then to add Paypal, I tried following codes, which were not helpful:
//$route['(:any)'] = 'paypal/index';
//$route['Paypal'] = 'paypal/index';
//$route['Welcome/Paypal/(:any)'] = 'welcome/index';
I tried them separately and also together but still I get this result:
404 Page Not Found
The page you requested was not found.
What I need to write, for using my Paypal controller as well?
If, for example, you want to go to the URL https://example.com/paypal you do not need a route if the Paypal controller has an index function.
If you wanted a way to "buy" a pair of socks that used the URL https://example.com/buy/socks but wanted to handle this request using the PayPal controller method buy($item), then you need a $route.
$route['buy/(:any)'] = 'paypal/buy/$1';
But you do not need a route if your "buy" URL is https://example.com/paypal/buy/socks
The only time you need to define a $route is when you want to deviate from CodeIgniter's controller/function[/arg1[/arg2[...]] URI pattern.
Your problems may not be route related. Be certain you followed the CodeIgniter rules for controller file and class naming? The name of the file must begin with an uppercase letter, i.e. Paypal.php and the class definition must match the file name exactly. i.e.
class Paypal extends CI_Controller {
What i got from your question is that you want the paypal controller to be rerouted or redirected to welcome controller.
if i got it right, you can simply do that with redirect .. in your paypal controller use redirect in your construct or index to redirect you to welcome controller
I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}
I would like all my URLs in codeigniter to start with the first URI segment to be passed to the controller as a parameter.
Here is my use case:
For the url: http://www.example.com/site/page/1
The "Site" would be a parameter passed to the controller "page", "1" is also a parameter (and anything after it).
Would a Mod_Rewrite be more appropriate than codeigniter routing?
You can write it in routes for particular page like
$route['site/page/(:num)'] = 'page/site/$1';
I am having trouble removing the controller name from my url path on my localhost.
i have this url - localhost:8888/localhost/site_name/
i have been able to remove index.php from the url using my htaccess similar to http://codeigniter.com/wiki/mod_rewrite so that:
localhost:8888/localhost/site_name/index.php/controller_name
is now:
localhost:8888/localhost/site_name/controller_name/
but i can't remove the controller name from the path so that:
localhost:8888/localhost/site_name/controller_name/function_name/
becomes:
localhost:8888/localhost/site_name/function_name/
I am using only one controller, and i have added:
$route['^(function_name1|function_name2|function_name3)(/:any)?$'] = 'controller_name/$0';
$route['^(?!ezstore|ezsell|login).*'] = "home/$0"; /*similar variation i tried*/
and other variations to my routes file but it does not have any effect. i also tried using the _remap function but that does not help in this case.
Any help will be appreciated! Thanks
You can use a wildcard route,
$route['(:any)'] = "controller_name/$1";
Then when if you go to http://localhost/function_one/param1
it will call the controller controller_name the function function_once and pass param1 as the first parameter.
nb: I must point out, using only one controller for an entire site does raise warning bells for me, you may want to get your code design checked out, but that's just me.
my journey of learning MVC continues and as hard as it is, I'm learning a lot of things I could never learn otherwise. Now I have faced the problem in routing. Currently I'm taking the $_SERVER["REQUEST_URI"] and get the controller and method and any args. When url is in format http://mysite.com/forum/thread/12/1123 there is no problem but I need to catch also requests like http://mysite.com/index.php?forum=12&&thread=1123.
I have read links in threads below but cannot get my head on QSA and I though I would better ask.
Thanks
mod_rewrite: Check for Custom query string in URL?
Rewrite url with query string in htaccess
I ended up writing something like before:
I redirect using htaccess
//No Controller specified in url (The current url is base url like http://example.com/hosanna_framework/)
if(!isset($_GET['base_url'])){
$url = $config["router"]["default_controller"];
}
//Controller is specified in url
else{
$url = $_GET['base_url'];
}