CakePHP: How to route Pagination's sort parameters? - php

So I'm trying to page items on my index page using the paginator and custom routes. It's all through the index action, but the index action can show items sorted by newest, votes, active or views. Right now, the URL looks like this:
items/index/sort:created/direction:desc
And if you aren't on page one, it looks like this:
items/index/sort:created/direction:desc/page:2
I'd like to use the router to have it look like this:
newest/
I can get that far with this route:
Router::connect(
'/newest/*',
array('controller'=>'items', 'action'=>'index', 'sort'=>'created', 'direction'=>'desc')
);
However, the pager links don't follow the route. As soon as you click next page, you're back to:
items/index/sort:created/direction:desc/page:2
How can I make this follow the router and give me what I want? Keep in mind, it's all from the same controller action, I'm trying to route the sort parameters of pagination basically.

For me your code is working (I've tested your example). Have you done something unusual with the paginator helper?
Here is my Routes:
Router::connect('/newest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'desc'));
Router::connect('/oldest/*',array('controller'=>'tests', 'action'=>'index', 'sort'=>'age', 'direction'=>'asc'));
And here are the urls which I've seen when I sort by age column:
http://localhost/cakephp/1.3.0/newest/page:1
http://localhost/cakephp/1.3.0/newest/page:2
http://localhost/cakephp/1.3.0/newest/page:3
And oldest:
http://localhost/cakephp/1.3.0/oldest/page:1
http://localhost/cakephp/1.3.0/oldest/page:2
http://localhost/cakephp/1.3.0/oldest/page:3
And it's working with all links in the pager (first, prev, 1,2,3 next, last).

You want to include the passed args I think. Something like this,
$this->params = $this->passedArgs();
Have a check here also, http://book.cakephp.org/view/46/Routes-Configuration
Otherwise I would extend the HTML Helper to create my own link method which read in the parameters from the url and created a link accordingly. Then you could manage your own links from your own helper :)
Don't forget that you need to have checks in the index action to deal with this. Personally I would be far more inclined to create an action in the controller for each of these.
function newest(){
}
function votes(){
}
function active(){
}
//etc

Related

Routing in Codeigniter 3

I'm relearning PHP and codeigniter but i can't help but notice how ugly my url looks.
So i have a pages controller: Say lets load homepage.
It would look like this:
public function home(){
$this->load->view('template/header');
$this->load->view('template/navbar');
$this->load->view('pages/home');
$this->load->view('template/footer');
}
I guess thats decent as my url will probably look like this:
http://localhost/ProjectName/index.php/pages/home
But whenever i click on a link that involves more data processing for example lets say my home has a list of users and i click on one to view their profile. I ofcourse have to pass something to identify which profile i am to load so my link would look like this:
http://localhost/OnlineStories/index.php/pages/view_userprofile/1
In my controller id have:
public function view_userprofile(){
$userid = $this->uri->segment(3);
//process data then load view
}
I'm not asking for any advanced stuff but i would like to ask simple things to improve my url like correct standard for naming my controller functions.
For example: on previous instances of loading my homepage my function name was view_home now its simply just home so its cleaner.
Maybe i could rename my view_userprofile to simply user_profile.
Anyway is there any better method to passing values from view to controller other than uri segment? its what ive been using so i never gave it much thought. I've seen some use get() but idk if its better than uri segment?
Or should i stick with the uri segment and keep my url as controller/method/parameter?
I haven't really explored post-project polishing in codeigniter so idk if theres a way to polish/change the url(just the name).
Talk about URL in CodeIgniter, you never mention about application/config/routes.php file. Have you seen it ..?
In routes.php file you can create your URL name and mapping it to the controller/function that you have
like this:
$route['user/update'] = 'user_controller/update_data_function'
so, you can call URL http://your.site/index.php/user/update to execute your update_data_function() in your User_controller.php

Create a Dynamic URL and Redirect to it in Laravel

So basically this is what I need. I have a router definition like this.
Route::get('view/{postId}', 'PostController#view');
Above router definition will get triggered if the url we request is www.domain.com/view/4. But what I want is I want to appear this url like www.domain.com/[category-of-post]/[titile-of-post] (Ex : www.domain.com/music/easy-chords-in-guitar).
In PostController, I will get the post using the id passed and thus can generate the url what I need. Here is the problem begins. As you can see, I need to redirect to a dynamic url which will look differently for each post. I want to redirect to this dynamic urls but these urls are not defined inside routes.php.
Is this possible in Laravel ?
In short, what I need is, I want to update Illuminate\Routing\RouteCollection::$route array with my dynamically generated url value with corresponding controller action in run time before I am invoking Redirect::to('someurl')
If you need further clarification, I will do it for sure. Please give me your suggestions.
it is simpler than you are thinking.
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
This should be the last route defined in your route list. Any other route should go upper than this one.
this will match www.domain.com/music/easy-chords-in-guitar
rest routes define as you want.
e.g.
Route::get('/',['uses' => 'FooController#home']);
Route::get('about',['uses' => 'FooController#about']);
Route::get('contact',['uses' => 'FooController#contact']);
Route::get('{category}/{title}',['uses' => 'FooController#bar']);
route :
Route::get('action/{slug}', 'HomeController#actionredeemvoucher')->name('home.actionredeemvoucher');
Function in controller:
public function actionredeemvoucher($slug)
{
print_r($slug);
}

Working with CodeIgniter routes?

I think this is a route issue but I'm not sure. I have a page with this URL:
siteurl.com/kowmanger/titles/titles/edit/$id
I'm trying to find out that when I'm on this page I load the titles page it says page not found so I need to tell it that the $id is just a paramter so I can use it to get the data of the title.
UPDATE :
So I decided to change my titles controller so that there's a edit and add function inside of the titles controller that way they dont' have separate controllers when they are in fact methods.
So now I have:
kansasoutalwwrestling.com/kowmanager/titles/titles - list of titles
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
kansasoutalwwrestling.com/kowmanager/titles/titles/edit/$id - edit form
I don't have any routes set up so far for this. For some reason though I"m getting the same page for both of these page.
kansasoutalwwrestling.com/kowmanager/titles/titles/add - addnew form
(right link url) kansasoutalwwrestling.com/kowmanager/titles/add -
addnew form
I need a route so that it'll show the correct url if the add method is accessed.
Also I need to set up a route so that if the correct edit link is accessed it sees the id attached to the end of the url and it'll accept it so that I can do a my database query to get the title data.
UPDATE: So to reiterate I have a module(subfolder) called titles. Inside of the module I have a controller called titles and inside of that controller I have 3 functions called index(), add(), edit().
I tried using Chris's suggestion on the routes but its not routing correctly. Also wanted to mention I'm using wiredesignz modular separation framework if that matters.
Any additional ideas?
Possible answer based on your post, not one hundred percent your entire structure but if i had to guess based off the post I would try this as my routes first..
$route['titles/titles/edit/(:any)'] = 'titles/titles/edit/$1';
$route['titles/titles/add'] = 'titles/titles/add';
$route['titles/titles'] = 'titles/titles';
$route['titles'] = 'titles/index';
Are you using custom routing in your configuration files ?
The general routing protocol used by codeigniter is like this:
domain.com/controller/methode/param1/param2/param3
This being said, your url
siteurl.com/kowmanger/titles/titles/edit/$id
corresponds to something like this :
class Kownmanger extends CI_Controller
{
public function titles($titles, $action, $id)
{
}
}
In case you are using sub-folders in your controllers folder, what I have just said will change, Could you please tell us what's your directory structure ?

Problem with pagination links with routing

i need to route all /home/college, /home/school etc to thehome controller's index action, with the following prototype.
function index($type="school"){
...
}
below is my routing definition
Router::connect('/home/:type',array('controller'=>'home','action'=>'index'),array('pass'=>array('type'),'type'=>'(college)|(school)'));
i am also using pagination inside it. But when i generate the next and previous links it's like below
http://mysite.com/home/index/school/page:2
How can i remove the 'index' from the link?
I'm not sure if you will have much luck removing index as it is the default action.
However, I'm surprised your route works as mode is undefined.
Router::connect('/home/:type',
array('controller'=>'home', 'action'=>'index'),
array('pass'=>array('type'), 'type'=>'(college|school)'));
Check out CakePHP Routes Configuration.
Alternatively, you could just create a 'dummy' home action, and call index with it.
function home($type="school"){
$this->setAction('index',$type);
}
http://api13.cakephp.org/class/controller#method-ControllersetAction

Controller/Parameter syntax?

I use CodeIgniter and let's say i build a blog where /posts shows all posts, and /posts/cool_news shows the article called cool_news.
I know the first one would only need a controller called Posts and a function called index(). But how about the second example? how to get the second parameter which can be anything?
Using the default routing:
You create another action in the Posts controller called cool_news()
If you want it to be passed as an argument to the index() action then you need to look at doing some custom routing.
This is untested but should look something like:
$route['posts/(:any)'] = 'posts/index/$1';

Categories