0

I'm struggling with the render() method in Laravel 5. When $whatever->render() is runned, it takes the controller method name as the route by default.

Example: When i run this command in DelasController@updateFilter, the pagination route is set to whatever.com/marketplace/updateFiler?page=2, which does not make a sense to me.

Problem: I want to keep the route as simple as whatever.com/marketplace?page=2.

Question: Can anybody gives me a hint on how to solve this?

Thank you for your time and a discussion. Looking forward for a reply.

  • When you visit `whatever.com/marketplace/updateFiler`, the pagination links on that page will be `whatever.com/marketplace/updateFiler?page=2` - why would you want it to redirect to a different route? If you only want a simple route, you could start with the simple route. eg. if you want the pagination links to visit `whatever.com/marketplace?page=2`, then make *that* your route for that page. – Kryten Jun 30 '15 at 16:07
  • Sure, there is async ajax call to updateFilter. That's the problem – user1974467 Jul 01 '15 at 09:08
  • I need to apply filters to DB/Eloquent results, than crate HTML representation of those, and generate pagination, so I can replace original content with filtered one. – user1974467 Jul 01 '15 at 09:10
  • Oh, you didn't mention that it's an AJAX call. In that case, I have a different approach for you. I'll write it up as an answer. – Kryten Jul 01 '15 at 13:59

1 Answers1

0

I have an application in which various paginated lists are displayed in "windows" on the page and are updated via AJAX calls to the server. Here's how I did it:

Set up a route to render the whole page, something like this:

Route::get('/marketplace', function ($arguments) {
    ....
});

Set up a route which will return the current page of the list. For example, it might be something like this:

Route::get('/marketplace/updateFiler', function ($arguments) {
    ....
});

In your Javascript code for the page, you need to change the pagination links so that, instead of loading the new page with the URL for the link, it makes the AJAX request to the second route. The Javascript could look something like this:

$('ul.pagination a').on('click', function (event) {
    // stop the default action
    event.stopPropagation();
    event.preventDefault();

    // get the URL from the link
    var url = $(event.currentTarget).attr('href');

    // get the page number from the URL
    var page = getURLParameterByName(url, 'page');

    $.get('marketplace/updateFiler', { page: page }, function (data){
        // do something with the response from the server
    });
});

The getURLParameterByName function is simply a helper that extracts the named parameter from a URL:

var getURLParameterByName = function (url, name, defaultValue) {
    // is defaultValue undefined? if so, set it to false
    //
    if (typeof defaultValue === "undefined") {
        defaultValue = false;
    }

    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(url);
    return results === null ? 
        defaultValue : 
        decodeURIComponent(results[1].replace(/\+/g, " "));
};

I adapted this code from an answer I found here on Stack Overflow: https://stackoverflow.com/a/901144/2008384.

Community
  • 1
  • 1
Kryten
  • 15,230
  • 6
  • 45
  • 68