Laravel Implicit Controller pagination - laravel-4

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

Related

New NICE URLs with 301s. How to make them work Together?

I have this old website URL structure:
site.com/folder/prod.php?cat=MAIN%20CAT%20&prodid1=123&prodtitle=PROD%20TITLE&subcat=SUB%20CAT
and real example will be something like:
site.com/folder/prod.php?cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS
here you can see that for the product page there are 4 variables: category, produt id, product title and sub category. Some of this variables were used to open a menu. And yes, the URL pulls variables with space and both lower and uppercase.
The new site url has a new structure:
site.com/x/product-title-prodid2
a rel example will be like:
site.com/x/mtx-amps-8888
Which is accomplish by using two variables (friendly slug + a second product id: prodid2) with the following code in the .htaccess
<IfModule mod_rewrite.c>
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^p/(.*)/$ product.php?prodid2=$1
RewriteRule ^p/(.*)$ product.php?prodid2=$1
</IfModule>
Internally we can get prodid2 if we have prodid1 from the same table, but not viceversa.
Everything works fine, but we now have to create 301 redirects and apparently since the same variables are not used in the old / new url, then it becomes tricky since apparently we have to create a single rule for the nice URL creation and the 301s?
We have tried adding the following to the htaccess:
RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]
and it works for only 1 product, but when adding 2 or more, the site goes down. I imaging this would be an infinite loop?
An alternative would be adding a:
ErrorDocument 404 /404.php
to get the URL and redirect to the page, but this would be ugly for SEs.
UPDATE:
Sorry for my lack of understanding on this topic, am very new to this.
The product has 2 important ids. For example:
MTX AMP (which is the actual product title) if listed in 3 categories will have 1 single prodid2 repeated and 3 different prodid1 (1 for each category). They all reside in the same table. So, if we have a prodid1 we can get the prodid2 which is right next to it in the db table.
The rule to get a nice URL on the new site is pulled using prodid2
RewriteRule ^p/(.*)$ product.php?prodid2=$1
which brings the complete value stored in the database. e.g. mtx-amps-8888 << this is a mix of a slug + the prodid2
complete url is:
site.com/p/mtx-amps-888
(the p is just a virtual forder and we take advantage of that variable to show the right page template)
So mtx-amps-888 are not 3 keys, these are generated when creating a product and saved all together in a single field in the db. They already include the separation - so this is not done in the htaccess.
The cat (key) value is really used to expand a menu used in the old site with, but to create the 301 redirect we would probably use prodid1 since we can match that value to get a prodid2. prodid2 is used as the main query to get the nice URL in the new site and its value will bring the nice URL stored in the db.
What makes sense from all my research would be the following:
<IfModule mod_rewrite.c>
Options +Indexes
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^p/(.*)$ product.php?prodid2=$1
RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=4444&prodtitle=MTX%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]
RewriteCond %{QUERY_STRING} ^cat=CAR%20AUDIO&prodid1=5555&prodtitle=BOSS%20AMPS&subcat=AMPS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]
RewriteCond %{QUERY_STRING} ^cat=CAR%20VIDEO&prodid1=6666&prodtitle=ALPINE%20DVDS&subcat=DVD%20PLAYERS$ [NC]
RewriteRule ite.com/folder/prod.php site.com/x/mtx-amps-8888? [R=301,L]
</IfModule>
Pls note that I removed a line from the main rewrite rule:
RewriteRule ^p/(.*)/$ product.php?prodid2=$1
This only assures that the user can also use / at the end of the URL: site.com/p/mtx-amps-888/
I also repeated the rewrite condition for the 301 redirects of 3 products, but i really have about 3K products to list here. If I keep 1, it will work but if I add 2, I believe a loop is created.
Hopefully this makes sense. You have no idea how important is for me to get this up and running, so my best wishes to those who can help :)
Just re-create the file /folder/prod.php and have php do the redirect. This is the easiest and cleanest solution.
<?php
$prodid1 = $_GET['prodid1'];
//calculate prodid2 based on prodid1, or use mysql to retreive the prodid2 belonging to prodid1
$prodid2 = $prodid1;//just for testing
$newpath = "/p/$prodid2/";
// redirect using 301
header("Location: http://{$_SERVER['HTTP_HOST']}{$newpath}");
header('HTTP/1.1 301 Moved Permanently');
?>

Joomla htaccess rewrite url - Parameter must index by a number - Why?

this is the firsttime a put a question here, so dont hard on me. Thank you.
I currently setup a joomla site. I create a page, and a new template, and a module, inside the template/index.php i call my module.
The original url that works is something like:
index.php/danh-sach-game?gt_name=game_mang_xa_hoi
danh-sach-game: is the page.
game-mang-xa-hoi: is the input parameter to the module.
everythings works find but i want to rewrite url to this:
danh-sach-game/game-mang-xa-hoi
So i created a .htaccess with content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^danh-sach-game/(.*)$ index.php/danh-sach-game?gt_name=$1 [L]
</IfModule>
Now this is time for "MAGIC"
if i enter the url:
danh-sach-game/game-mang-xa-hoi
then Joomla push a message "An error has occurred.The requested page cannot be found."
But if i index the parameter by a number like this:
danh-sach-game/1-game-mang-xa-hoi (note: the number 1).
Then it works finds. Any paremeter index by a number will work find.
I rewrite url to a test file (replace index.php by test.php) than the page test.php receive the parameter as usuas, with or without number index the parameter.
This is because Joomla and most of the Joomla extensions uses ID column of the content as an identifier to look up the database. Some of the SEF tools (for example AceSef, sh404Sef) provide the facility to lookup using the alias name (the text after the number and hyphen) however with additional cost of database queries (they will in turn query for proper url internally).
The number in the last part of the URL will be processed and passed as ID of the particular page/content that you are viewing. This is done in the particular component's router.php file. So check the router.php file of the component you are using to check how the url gets parsed.

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.

CodeIgniter, multiple possible segments in one route

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

Getting the original REQUEST_URI when using mod_rewrite AND mod_proxy

I'm using a custom.conf file for rewrites and codeigniter for some features of the site, mainly the articles.
My original url gets rewritten, so I have http://example.com/article-a101, this uses the custom.conf file to rewrite to codeigniter/article/read/101. I think I must send this as a proxy call using the [P] flag in mod_rewrite to make it rewrite again in codeigniters .htaccess file. Once it hits the code igniter .htaccess, it uses that mod rewrite structure to call the index file and use the article controller and the read function sending in the 101 as the parameter.
What I'm trying to figure it is how do I get the original url in the address bar as its not in the $_SERVER variable. Since I use the [P] on the first rewrite, request_uri has codeigniter/article/read/101.
custom.conf
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2 [P,L]
codeigniters .htaccess, fairly basic
RewriteRule ^(.*)$ index.php?/$1 [L]
Here's my current solution that I know there must be a better method for
RewriteRule ^/([_a-zA-Z0-9-]+)-a([0-9]+)$ /codeigniter/article/read/$2?orig_url=%{REQUEST_URI}&%{QUERY_STRING} [P,L]
This stays hidden from the user, and I can access the original url through the query string, but doesn't seem like an elegant solution.
I'm pretty sure you cant do it any other way with mod_rewrite
but you could do it with codeigniter routing.
$route['^([_a-zA-Z0-9-]+)-a([0-9]+)$'] = "article/read/$2";
assuming your controller is named article and your function is named read
if you visited /article-a101
then $this->uri->uri_string(); would return article-a101 (the original url, which should be in your url bar now)
and $this->uri->ruri_string(); would return article/read/101 (where you actually are)

Resources