Codeigniter Routes Conflict - codeigniter

I am using codeigniter to create a restapi and I am having some problems with routes.
Here is the equation:
I have to navigate /users/ to index function,
I have to navigate /users/{MongoId} to /users/show/{MongoId},
I have to navigate /users/function to /users/function.
And here, my routes:
$route['api/users/do_login'] = "api/users/do_login";
$route['api/users/(.*)'] = "api/users/show/$1";
When I remove the (.*) routing (or both of them), my do_login function works successfully. But not my api-index function because Codeigniter takes MongoId as function name and fails.
When I write it back (or both), my index function works successfully, but my login doesn't.
Because it tries to send function name to show function as parameter.
Can you please help me to fix that?

Reverse the order of routes, CodeIgniters routes are prioritized.
You seek this structure:
$route['api/users/(:any)'] = "api/users/show/$1";
$route['api/users/do_login'] = "api/users/do_login";
also use (:any) instead of (.*) they are the same.
CodeIgniter Routing

Here, the working routes.
$route['api/users/do_login/(:any)'] = "api/users/do_login/$1";
$route['api/users/(:any)'] = "api/users/show/$1";

Related

Codeigniter router get parameter

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

Laravel Route::get() function and parameter confusing

Hello everyone I just installed laravel4 and spend two days trying to make the first step. Now I made it but I'm confused about the Route::get() function and his paremeters.
I installe laravel directly in
/opt/lampp/htdocs/laravel
then follow tutorial to create file
userform.php
into app/views, then add following codes into routes.php
Route::get('userform', function()
{
return View::make('userform');
});
. Then I go to
/localhost/laravel/public
to see welcome page, and
/localhost/laravel/public/userform
to see the form defined in the view/userform.php.
Q1: According to chrome dev tools, i see in the html page, the form action is
http://localhost/laravel/public/userform
but there is nothing under public but
index.php, favicon.ico packages robots.txt
Q2: for
Route::get('userform', function()
{
return View::make('userform');
});
what is the first "userform" represent?? according the official tutorial, it's supposed to be url, but what is the former part?
for this line
return View::make('userform')
I guess "userform" referes to the file /app/views/userform.php, right?
The .htaccess file in the public directory is responsible for funnelling all incoming requests through the index.php file. This allows Laravel to grab the URI and match it to the route you defined and eventually return to you the view you made.
So you request localhost/laravel/public/userform, the request is funnelled through index.php and Laravel is booted. Laravel picks off the userform part of the URI and matches it against your defined routes. It finds the route you defined and fires it and returns the response.
You're spot on with what you were thinking with your second question as well. When you call View::make the first argument is the name of the view you want to "make". If you named your view app/views/forms/user.php then you would return it like so in your route:
return View::make('forms.user');
Or you could use a slash:
return View::make('forms/user');

Method name as Route - codeigniter

How can I do the following in routes dynamically?
$route['notifications'] = 'admin/notifications';
$route['categories'] = 'admin/categories';
This means that any method name under my controller is the landing page.
I don't want the admin controller to appear in the url.
I would simply use $CI->router->method in routes by I can't use get_instance in routes config.
What do I have to do?
Thanks!
It's difficult to get dynamic routes since at that point there's not much of CodeIgniter loaded.
I use the following to move all the methods of a controller to the first segment:
$route['(?!(api|account|more))(\w+)/(.*?)'] = "admin/$2/$3";
$route['(?!(api|account|more))(\w+)'] = "admin/$2";
Where api|account|more are routes being ignored.

Remove controller name from codeigniter 2 url path

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.

How to convert _ to - in urls for codeigniter?

Been trying to learn codeigniter, 1 problem I had though is that if I had a function say
top_10()
It would mean that my urls will be something like
..../top_10/
Which is fine but I prefer - more than _ for urls. I tried changing my function names to top-10(), but it results in a syntax error it seems (even if it doesn't it results in a ugly function name), Is there a way to let codeigniter auto converts all the _ in my controller functions to - when it comes to the urls?
.../top_10/ -> .../top-10/ for all other similar controller functions.
You can use the URI routing rule
something like :
$route['controller/top-10'] = "controller/top_10";
Add this line in the routes.php file inside application/config/routes.php
Here controller/top-10 will be the part of your url which will be routed to the desired function provided by on the right hand side.
also if you aren't using mod_rewrite to rewrite your url then your URL will appear some thing like
http://localhost/index.php/controller/top-10
Inside your application/config/routes.php folder if there isn't a line for translating uri dashes then add this line: $route['translate_uri_dashes'] = TRUE;
If $route['translate_uri_dashes'] already exists, simply set it's value to true.

Resources