Giter Site home page Giter Site logo

jcergolj / after-action-redirect-pagination-url-for-laravel Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 40 KB

saved index url with request params and after POST, UPDATE, DELETE action redirects back to saved url

License: MIT License

PHP 100.00%
laravel-package redirect-urls

after-action-redirect-pagination-url-for-laravel's Introduction

Why

Watch the video

The After Action Redirect Pagination URL for Laravel package provides a solution for two issues that developers may encounter when working with pagination in their Laravel application. These issues include losing query parameters and the current page when editing an item and being redirected to the first page of the index, as well as being redirected to an empty last page after deleting the last item on the last page.

Installation

composer require jcergolj/after-action-redirect-pagination-url-for-laravel

Saving and Redirecting to Last Index URL with Query Parameters

This package allows you to save the full URL, including any query parameters, of the index page in the intended URL session. After a create or update action, you can use the to_intended_route helper method to be redirected to the same page. You can also include or exclude routes from the intended URL saving mechanism for greater customization.

To use this feature, add the following code to your controller:

    return to_intended_route('users.index');

By default, to_intended_route will redirect to the last intended URL. If no URL was intended, it will redirect to the users.index route.

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        return view('users.index', [
            'users' => User::paginate(10),
        ]);
    }

    public function update(Request $request, User $token)
    {
        $user->update([...]);

        return to_intended_route('users.index');
    }
}

Include and Exclude routes

To include or exclude routes from the intended URL saving mechanism, create a SetIntendedUrlMiddleware class in the App\Http\Middleware namespace. In this class, you can specify the routes that should be excluded or included using the $excludedRoutes and $includedRoutes arrays.

<?php

namespace App\Http\Middleware;

use Jcergolj\AfterActionRedirectUrlForLaravel\Http\Middleware\SetIntendedUrlMiddleware as Middleware;

class SetIntendedUrlMiddleware extends Middleware
{
    /**
     * Routes that should be excluded.
     * @var array
     */
    public $excludedRoutes = [
    ];

    /**
     * Routes that should be included.
     * @var array
     */
    public $includedRoutes = [
    ];
}

Redirect to Last Page with Items after Deleting Last Item on Last Page

When you delete the last item on the last page of your Laravel app, you may encounter an issue where you are redirected to an empty last page. The After Action Redirect Pagination URL for Laravel package solves this problem by redirecting you to the last page with items.

To use this feature, add the following code after your paginated query:

redirect()->redirectIfLastPageEmpty($request, /** paginated results */);

This will check whether there are any items on the last page of your paginated results. If there are no items, it will redirect you to the last page that has items.

Here is an example of how you might use this in your controller's index method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
        $users = User::paginate(10);

        // continue executing the code, or redirect back with page set to paginator last page
        redirect()->redirectIfLastPageEmpty($request, $users);

        return view('users.index', [
            'users' => $users,
        ]);
    }
}

after-action-redirect-pagination-url-for-laravel's People

Contributors

jcergolj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

after-action-redirect-pagination-url-for-laravel's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.