CodeIgniter, multiple possible segments in one route - codeigniter

Ok, so maybe that title is a bit confusing. What I'd like to do is trap any info, if any after the first URI segement if that segment is something specific and forward that on to another controller. Here's my routes file:
$route['default_controller'] = "main";
$route['404_override'] = '';
$route['testroute'] = "main";
So, what this does right now is, if I got to mydomain.com/testroute it shows me the default page, which it should. However, what I'd like is if I go to mydomain.com/testroute/testmethod/ where testmethod is a method in the main controller I'd like it to forward that as well. So basically I'd like it to route to the main controller regardless of if there are more segments after the testroute, but if there are they should get passed as method calls of the main controller.

Simply catch the parameter given and pass it to the controller e.g. like this:
$route['testroute/(:any)'] = "main/$1";
(:any) actually catches any type of string. There are other selectors, as well. More on this topic can be found here.
Edit (answer to your comment):
If you want a general route to the index() method of your main controller, just add both routes:
$route['testroute'] = "main";
$route['testroute/(:any)'] = "main/$1";

Uh, what? That's how CodeIgniter's controllers work already. Show us some code? What isn't working about it?
Have you configured URL rewriting in your .htaccess file so you don't need the /index.php/testroute in the URL?
Open .htaccess, and try this code if you haven't already rewritten this:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
If you haven't configured rewriting then it won't work without that^, but you can access like mydomain.com/path/to/ci/index.php/testroute/testmethod

Related

Laravel Implicit Controller pagination

I have one of routes defined like this in laravel routes file.
Route::controller('login/home/admin/', 'AdminController');
But it seems laravel pagination does not work in methods. So i changed Route to allow page variable like this in one of the methods.
Route::get('login/home/admin/users/{page}', 'AdminController#getUsers');
Route::controller('login/home/admin/', 'AdminController');
Now problem is login/home/admin/users/2 loads but pagination does not work and if I try this login/home/admin/users?page=2 I am redirected.
EDIT:
My method is defined like this and does not work.
public function getUsers(){
var_dump( Input::get('page') ); // Returns NULL
$users = User::paginate(10);
...
...
}
and in view
...
{{ $users->links() }}
...
This view generates pagination but only first page works. Page 2 and other pages show records of page 1.
I doubt its because Input::get('page') is not working for some reason.
EDIT 2
None of following routes worked for me.
Route::get('login/home/admin/users/', 'AdminController#getUsers');
Route::controller('login/home/admin/', 'AdminController');
and
Route::get('login/home/admin/users/{page}', 'AdminController#getUsers');
Route::controller('login/home/admin/', 'AdminController');
and
Route::controller('login/home/admin/', 'AdminController');
Change your Route to
Route::get('login/home/admin/users/', 'AdminController#getUsers');
Laravel Pagination automatically takes care of your page variable in request
Here is the extract for Laravel documentation:
There are several ways to paginate items. The simplest is by using the
paginate method on the query builder or an Eloquent query. The
paginate method provided by Laravel automatically takes care of
setting the proper limit and offset based on the current page being
viewed by the user. By default, the current page is detected by the
value of the ?page query string argument on the HTTP request. Of
course, this value is automatically detected by Laravel, and is also
automatically inserted into links generated by the paginator.
ok this was something silly in htaccess that was not letting ?page=x or any other GET variable pass to code. Compared my htaccess to real htaccess of Laravel 4.2 and there was something different.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
#RewriteRule ^(.*)$ index.php?/$1 [L] <-- CULPRIT

Set a custom url for Codeigniter Controller

Please can any one suggest how to shorten the url
http://localhost:8080/MyWebApp/index.php/Cpanel_control/
to
http://localhost:8080/MyWebApp/Cpanel
in Codeigniter using routes.
I tried it in this way
$route['Cpanel'] = "MyWebApp/index/Cpanel_control";
But did not work
To remove index.php from your url in CI, you need .htaccess file.
Check this out https://gist.github.com/philipptempel/4226750
I'm assuming Cpanel_control is a valid controller.
For the routing, you can have this in your routes settings
$route['Cpanel'] = "Cpanel_control";
To avoid any other issues, make sure base_url in config file is set thus
$config['base_url'] = "http://localhost:8080/MyWebApp";
New Approach
Routing:
In some instances, however, you may want to remap this relationship so
that a different class/method can be called instead of the one
corresponding to the URL.
A short excerpt from the CI doc shows that you can't use routing here. Instead you should go for a mod_rewrite rule (.htaccess)
RewriteEngine On
RewriteRule ^index/Cpanel_control/(.*) Cpanel/$1 [R]
Old approach
According to the corresponding documentation, the paths are both not absolute. Furthermore, you need to set the "from"-URI as the array key and the "to" URI as the string. If you want to route index/Cpanel_control to Cpanel, you need to swap the URIs of you example.
So this would be correct:
$route['index/Cpanel_control'] = "Cpanel";

Rewriting url with the following regular expression

I'm trying to do a mod rewrite to get this url: localhost/test/index.php?hello
I created a file for this page called hello.php and it is in the folder /test
To clarify, I have another page that has a link to my hello.php, but what is the correct url so I can display localhost/test/index.php?hello in the url when I click the link to access my hello.php page.
The following doesn't seem like it is right:
RewriteRule ^.*$ index.php?$1 [L]
Try this if you want to just do php files.
RewriteEngine on
RewriteRule (.*)\.php$ /index.php?$1 [L]
To clarify what my answer does. It gives you more friendly URLs which it sounded like what your were asking for.
So you can use localhost/hello.php and it will be internally redirect to /localhost/index.php?hello. Internally means they will never see localhost/index.php?hello and will always see localhost/hello.php in their browser.
You can do any URL and it will rewrite to a php file. e.g. localhost/index.php?newpage and you can use /localhost/newpage.php
Hope that is clearer.
EDIT
You want the reverse but I don't know how your PHP is constructed but query strings are typically field/value pairs. For example name=john, page=contact, action=hello etc. You have index.php?hello but hello has no content or value.
That's probably why you're having such a hard time even re-writing your URL. Using $_GETwould require a value.
So what I would do, is if your URL was like this using field/value pairs
index.php?action=hello
Then in the index.php file you could do something like
$action = $_GET["action"];
if($action == "hello"){
//show contents of hello, include a page or whatever
}
Once you have good URLs it would be easy to rewrite it.
So if the URL that you want shown is like
index.php?action=hello and you want to redirect it to hello.php
Your .htaccess would look like this
RewriteRule ^action=([^/]+) /$1.php [R,L]
That would redirect it to the php file. If you don't want to show the redirection and keep it an internal redirect you can remove the R flag and just keep [L].
I personally don't want the user to see really long query strings URL example. mysite.com?page=music&artist=someartist&title=sometitle
So all my URL's are rewritten to be shorter and friendlier like my original answer.
you don't need .htaccess for 2. as far as you're using GET parametr - use it in index.php:
if (isset($_GET['hello'])) include('hello.php');
this will show the contents of hello.php inside index.php

Codeigniter: Deep level arguments

I think, this probably is novice question, but I am novice in CodeIgniter :)
Well here is the problem, I'm trying to make categories and subcategories (dynamically generated) for store, and the main problem is that, I could manage to set different options to main category with _remap function in my controller. But, if I am trying to get deeper, then the same _remap function applies, and I am stuck there.
For example, the main category uri is http://project.com/store/fruits/, but for the subcategory, of course - http://project.com/store/fruits/apples.
I want to apply different view to 3rd segment, and still be able to control main category (fruits) with _remap function.
I want to use one controller over and over, but I think, it must be crazy to copy and paste the same function content for all subcategories (hundreds of them, disguised).
Maybe there is some way to do that, but I can't find out how... Help here! :)
/Rob
Not sure why you would need the _remap function.
If "store" is your controller, you can set each top level category as a function inside store. What's passed (via the remaining URI) to each function would be the subcategories and those can be captured and looked up in a database to get the info you need. Something like this:
Function fruits(){
$sub1 = $this->uri->segment(3); // this will be apples, etc...
...
// if it's empty - call viewX
// else call db lookup for $sub1 data here and pass to viewY
}
Or...If you used .htaccess, you could reroute like this:
RewriteCond %{REQUEST_FILENAME} store.*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/([a-zA-Z]+)/([a-zA-Z]+)$ store/someFunction/cat=$1&subcat=$2 [L]
This is for illustration purposes - it needs to be modified to work in your environment.
Thanks, jco for your effort, but actually, I found _remap() function working pretty well for my needs.
I created public _remap() function with two arguments - $first_level and $next_levels, and then I controlled everything after these $next_levels given information.

403 forbidden with URI routing in Codeigniter

I'm trying to set up routing to a page on my site with Codeigniter, but I just get a 403. I can't understand why.
Code in the routes config file is:
$route['photo/(:num)'] = "viewphoto/view/$1";
$route['photo'] = 'photo';
$route['photos'] = "photospage/index";
$route['photos'] = 'photos';
$route['default_controller'] = 'homepage';
$route['homepage'] = 'homepage';
When going to mysite.com/photo/2 (for example) it works fine, as does the homepage. But when going to mysite.com/photos I just get a 403 Forbidden error message.
I can't work it out, the routing is set to exctly the same as the mysite.com/photo/2 routing.
The controller it's pointing to is called photospage and the function inside it is called index.
If I go to mysite.com/photos/index it works though...
Any help is most appreciated :)
EDIT:
Change the routes config file to the following but it still doesn't work when I go to mysite.com/photos. I changed the controller function to a 'view' instead of 'index' but it still won't work :(
$route['photos'] = "photos/view";
$route['photo/(:num)'] = "viewphoto/view/$1";
$route['default_controller'] = 'homepage';
homepage and photo/$id still work fine though.
Your rewrite rule should be:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
You have to remove your index.php entry from your /application/config/config.php
Verify that is setup correctly the 403 forbidden error might be due to a faulty .htaccess rewrite.
OK, let's take a look at these one by one (assuming your domain is 'example.com' and you have 'index.php' hidden via '.htaccess'):
$route['photo/(:num)'] = "viewphoto/view/$1";
This will grab any url like this: http://example.com/photo/36 and route it to the /application/controllers/viewphoto controller, and call the view method and pass it 36 as the parameter.
$route['photo'] = 'photo';
This will grab any url like this: http://example.com/photo and route it to the /application/controllers/photo controller, and call the index method with no parameter.
$route['photos'] = "photospage/index";
This will grab any url like this: http://example.com/photos and route it to the /application/controllers/photospage controller, and call the index method with no parameter.
$route['photos'] = 'photos';
This will grab any url like this: http://example.com/photos and route it to the /application/controllers/photos controller, and call the index method with no parameter.
This route will never get called because it duplicates the one right before it
$route['default_controller'] = 'homepage';
This will grab any url that hasn't been caught thus far and route it to the /application/controllers/homepage controller, and call the index method with no parameter.
$route['homepage'] = 'homepage';
This will grab any url like this: http://example.com/homepage and route it to the /application/controllers/homepage controller, and call the index method with no parameter.
My guess is this has to do with not having an index method in the controllers. If that is not the case, then we would need to see the contents of the .htaccess file.
This problem can occur if you have a directory with a name identical to the route that throws the 403 error. Delete or rename the directory and the route will work.

Resources