├── .github └── workflows │ └── run-tests.yml ├── .styleci.yml ├── LICENSE.md ├── README.md ├── UPGRADING.md ├── composer.json ├── resources ├── _ide_helper_macros.php └── lang │ └── en │ └── messages.php └── src └── LaravelRedirectResponseMacrosServiceProvider.php /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: "Run Tests - Current" 2 | 3 | on: [push, pull_request] 4 | 5 | jobs: 6 | test: 7 | 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | php: [8.3, 8.2, 8.1, 8.0] 13 | laravel: ["^12.0", "^11.0", "^10.0", "^9.0", "^8.12"] 14 | dependency-version: [prefer-lowest, prefer-stable] 15 | include: 16 | - laravel: ^12.0 17 | testbench: 10.* 18 | - laravel: ^11.0 19 | testbench: 9.* 20 | - laravel: ^10.0 21 | testbench: 8.* 22 | - laravel: ^9.0 23 | testbench: 7.* 24 | - laravel: ^8.12 25 | testbench: ^6.23 26 | exclude: 27 | - laravel: ^8.12 28 | php: 8.3 29 | - laravel: ^10.0 30 | php: 8.0 31 | - laravel: ^11.0 32 | php: 8.0 33 | - laravel: ^11.0 34 | php: 8.1 35 | - laravel: ^12.0 36 | php: 8.0 37 | - laravel: ^12.0 38 | php: 8.1 39 | 40 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} 41 | 42 | steps: 43 | - name: Checkout code 44 | uses: actions/checkout@v4 45 | 46 | - name: Get Composer Cache Directory 47 | id: composer-cache 48 | run: | 49 | echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT 50 | 51 | - uses: actions/cache@v4 52 | with: 53 | path: ${{ steps.composer-cache.outputs.dir }} 54 | key: ${{ runner.os }}-composer-${{ matrix.os }}-php-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }} 55 | restore-keys: | 56 | ${{ runner.os }}-composer- 57 | 58 | # - name: Cache dependencies 59 | # uses: actions/cache@v4 60 | # with: 61 | # path: ~/.composer/cache/files 62 | # key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} 63 | 64 | - name: Setup PHP 65 | uses: shivammathur/setup-php@v2 66 | with: 67 | php-version: ${{ matrix.php }} 68 | extensions: curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, iconv 69 | coverage: none 70 | 71 | - name: Install dependencies 72 | run: | 73 | composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "symfony/console:>=4.3.4" "mockery/mockery:^1.3.2" --no-interaction --no-update 74 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 75 | 76 | - name: Display PHP version 77 | run: php -v | grep ^PHP | cut -d' ' -f2 78 | 79 | - name: Execute tests 80 | run: vendor/bin/phpunit 81 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr12 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) F9WebLtd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://banners.beyondco.de/Laravel%20Redirect%20Response%20Macros.png?theme=light&packageManager=composer+require&packageName=f9webltd%2Flaravel-redirect-response-macros&pattern=brickWall&style=style_1&description=Some+super+useful+redirect+response+macros+to+simplify+your+Laravel+application&md=1&showWatermark=0&fontSize=100px&images=code) 2 | 3 | 4 | [![Packagist Version](https://img.shields.io/packagist/v/f9webltd/laravel-redirect-response-macros?style=flat-square)](https://packagist.org/packages/f9webltd/laravel-redirect-response-macros) 5 | [![Run Tests](https://github.com/f9webltd/laravel-redirect-response-macros/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/f9webltd/laravel-redirect-response-macros/actions/workflows/run-tests.yml) 6 | [![StyleCI Status](https://github.styleci.io/repos/278581318/shield)](https://github.styleci.io/repos/278581318) 7 | [![Packagist License](https://img.shields.io/packagist/l/f9webltd/laravel-redirect-response-macros?style=flat-square)](https://packagist.org/packages/f9webltd/laravel-redirect-response-macros) 8 | 9 | # Laravel Redirect Response Macros 10 | 11 | Some super useful redirect response macros to simplify your Laravel application. 12 | 13 | ## Requirements 14 | 15 | - PHP `^8.0` 16 | - Laravel `^8.12`, `^9.0`, `^10.0`, `^11.0` or `^12.0` 17 | 18 | ### Legacy Support 19 | 20 | For legacy PHP / Laravel support, use package version [`1.1.6`](https://github.com/f9webltd/laravel-redirect-response-macros/tree/1.1.6) 21 | 22 | ## Installation 23 | 24 | ``` bash 25 | composer require f9webltd/laravel-redirect-response-macros 26 | ``` 27 | 28 | The package will automatically register itself. 29 | 30 | Optionally publish language files by running: `php artisan vendor:publish` and selecting the appropriate package. 31 | 32 | ## Documentation 33 | 34 | This package allows for concise controller redirections by setting default flash data. It works as Laravels `RedirectResponse` class is "macroable". 35 | 36 | For example the packages allows: 37 | 38 | ``` php 39 | public function store(FormRequest $request) 40 | { 41 | // create record ... 42 | return redirect()->route('posts.index')->created(); 43 | } 44 | ``` 45 | 46 | ... instead of: 47 | 48 | ``` php 49 | public function store(FormRequest $request) 50 | { 51 | // create record ... 52 | return redirect()->route('posts.index')->with('success', 'The record was successfully created'); 53 | } 54 | ``` 55 | 56 | The former is of course much more concise and readable. 57 | 58 | The package specifies several custom `RedirectResponse` macros that can be used on any of the native Laravel helpers that return the redirect response object. 59 | 60 | The following methods are available. 61 | 62 | #### `success()` 63 | 64 | Flash message key: `success` 65 | Pass a message string to the macros. 66 | 67 | ``` php 68 | public function update(FormRequest $request, $id) 69 | { 70 | return back()->success('Everything is great!'); 71 | } 72 | ``` 73 | 74 | #### `info()` 75 | 76 | Flash message key: `info` 77 | Pass a message string to the macros. 78 | 79 | ``` php 80 | public function update(FormRequest $request, $id) 81 | { 82 | return back()->info('Some information ...'); 83 | } 84 | ``` 85 | 86 | #### `danger()` 87 | 88 | Flash message key: `danger` 89 | Pass a message string to the macros. 90 | 91 | ``` php 92 | public function update(FormRequest $request, $id) 93 | { 94 | return back()->danger('That action just is impossible!'); 95 | } 96 | ``` 97 | 98 | #### `warning()` 99 | 100 | Flash message key: `warning` 101 | Pass a message string to the macros. 102 | 103 | ``` php 104 | public function update(FormRequest $request, $id) 105 | { 106 | return back()->warning('This could be risky ...'); 107 | } 108 | ``` 109 | 110 | There are further helper method available, that set the same type of flash data, but in a more readable manner: 111 | 112 | #### `created()` 113 | 114 | Flash message key: `success` 115 | Default message: `The record was successfully created` 116 | 117 | ``` php 118 | public function store(FormRequest $request) 119 | { 120 | // create record ... 121 | return redirect()->route('posts.index')->created(); 122 | } 123 | ``` 124 | 125 | Alternatively pass a url to display an message with a link to view the created record: 126 | 127 | ``` php 128 | public function store(FormRequest $request) 129 | { 130 | // create record ... 131 | return redirect()->route('posts.index')->created( 132 | route('posts.edit', $post) 133 | ); 134 | } 135 | ``` 136 | 137 | The flashed message will now be: `The record was successfully created. View inserted record`. 138 | 139 | #### `updated()` 140 | 141 | Flash message key: `success` 142 | Default message: `The record was successfully updated` 143 | 144 | ``` php 145 | public function update(FormRequest $requestm int $id) 146 | { 147 | // update record ... 148 | return back()->updated(); 149 | } 150 | ``` 151 | 152 | To set a custom message, pass the desired text to the `updated()` function. 153 | 154 | #### `deleted()` 155 | 156 | Flash message key: `success` 157 | Default message: `The record was successfully deleted` 158 | 159 | ``` php 160 | public function update(Post $post) 161 | { 162 | $posts->delete(); 163 | 164 | return redirect()->route('posts.index')->deleted(); 165 | } 166 | ``` 167 | 168 | To set a custom message, pass the desired text to the `deleted()` function. 169 | 170 | #### `error()` 171 | 172 | Flash message key: `error` 173 | Specific message text should be passed. 174 | 175 | ``` php 176 | public function index() 177 | { 178 | // code ... 179 | return redirect()->route('dashboard')->error('You cannot do this thing!'); 180 | } 181 | ``` 182 | 183 | The function can detect the presence of exception object and call `getMessage()` as required: 184 | 185 | ``` php 186 | public function index() 187 | { 188 | try { 189 | $service->run(); 190 | } catch (Exception $e) { 191 | return redirect()->route('dashboard')->error($e) 192 | } 193 | } 194 | ``` 195 | 196 | #### `errorNotFound()` 197 | 198 | Works in the same way as the `error()` macro and is intended to make controllers more concise. 199 | 200 | The default message is `Sorry, the record could not be found.`. 201 | 202 | #### `authorized()` 203 | 204 | Flash message key: `success` 205 | Default message: `Welcome back, you have been securely logged in` 206 | 207 | A custom message can optionally be provided. 208 | 209 | #### `unAuthorized()` 210 | 211 | Works in the same way as the `error()` macro and is intended to make controllers more concise. 212 | 213 | The default message is `You do not have permission to perform that action`. 214 | 215 | ## IDE Autocompletion within PHPStorm 216 | 217 | Autocompletion of "macroable" classes with PHPStorm currently difficult, which is a shame given how useful macroable classes actually are. 218 | 219 | At present, the following process will trigger autocompletion in PHPStorm: 220 | 221 | - Copy [resources/_ide_helper_macros.php](resources/_ide_helper_macros.php) to a location within your project to allow PHP storm to index the additional class methods 222 | - Optionally add `_ide_helper_macros.php` to your `.gitignore` file 223 | 224 | ## Contribution 225 | 226 | Any ideas are welcome. Feel free to submit any issues or pull requests. 227 | 228 | ## Testing 229 | 230 | ``` bash 231 | composer test 232 | ``` 233 | 234 | ## Security 235 | 236 | If you discover any security related issues, please email rob@f9web.co.uk instead of using the issue tracker. 237 | 238 | ## Credits 239 | 240 | - [Rob Allport](https://github.com/ultrono) for [F9 Web Ltd.](https://www.f9web.co.uk) 241 | 242 | ## License 243 | 244 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 245 | 246 | -------------------------------------------------------------------------------- /UPGRADING.md: -------------------------------------------------------------------------------- 1 | # Upgrading 2 | 3 | ## From 1.x to 2.x 4 | 5 | - The package now requires PHP `^8.0` and Laravel `^8.12` / `^9.0`, `^10.0` or `^11.0` 6 | - No further changes are required, the API remains the same 7 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "f9webltd/laravel-redirect-response-macros", 3 | "description": "Some useful redirect response macros for your Laravel application", 4 | "keywords": [ 5 | "laravel", 6 | "laravel redirect", 7 | "redirect response macros", 8 | "laravel macros" 9 | ], 10 | "homepage": "https://github.com/f9webltd/laravel-redirect-response-macros", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Rob Allport", 15 | "email": "rob@f9web.co.uk", 16 | "homepage": "https://www.f9web.co.uk", 17 | "role": "Developer" 18 | } 19 | ], 20 | "require": { 21 | "php": "^8.0", 22 | "illuminate/support": "^8.12|^9.0|^10.0|^11.0|^12.0" 23 | }, 24 | "require-dev": { 25 | "orchestra/testbench": "^6.23|^7.0|^8.0|^9.0|^10.0", 26 | "phpunit/phpunit": "^9.4|^10.1|^11.5.3" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "F9Web\\LaravelRedirectResponseMacros\\": "src" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "F9Web\\LaravelRedirectResponseMacros\\Tests\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "vendor/bin/phpunit" 40 | }, 41 | "config": { 42 | "sort-packages": true 43 | }, 44 | "extra": { 45 | "laravel": { 46 | "providers": [ 47 | "F9Web\\LaravelRedirectResponseMacros\\LaravelRedirectResponseMacrosServiceProvider" 48 | ] 49 | } 50 | }, 51 | "minimum-stability": "dev", 52 | "prefer-stable": true 53 | } 54 | -------------------------------------------------------------------------------- /resources/_ide_helper_macros.php: -------------------------------------------------------------------------------- 1 | 'The record was successfully created', 6 | 'created' => 'The record was successfully created. View inserted record.', 7 | 'updated' => 'The record was successfully updated.', 8 | 'deleted' => 'The record was successfully deleted.', 9 | 'not-found' => 'Sorry, the record could not be found.', 10 | 'authorized' => 'Welcome back, you have been securely logged in', 11 | 'un-authorized' => 'You do not have permission to perform that action', 12 | 13 | ]; 14 | -------------------------------------------------------------------------------- /src/LaravelRedirectResponseMacrosServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadTranslationsFrom(__DIR__ . '/../resources/lang/', 'f9web-laravel-redirect-response-macros'); 18 | 19 | $this->publishes( 20 | [ 21 | __DIR__ . '/../resources/lang' => resource_path('lang/vendor/f9web-laravel-redirect-response-macros'), 22 | ] 23 | ); 24 | 25 | $this->registerMacros(); 26 | } 27 | 28 | private function registerMacros() 29 | { 30 | $this->registerHelper(); 31 | 32 | foreach (['success', 'info', 'danger', 'warning'] as $type) { 33 | RedirectResponse::macro( 34 | $type, 35 | function ($message) use ($type) { 36 | return $this->with($type, $message); 37 | } 38 | ); 39 | } 40 | 41 | $key = 'f9web-laravel-redirect-response-macros::messages.'; 42 | 43 | RedirectResponse::macro( 44 | 'created', 45 | function ($route = null) use ($key) { 46 | $message = null === $route 47 | ? trans("{$key}created-min") 48 | : trans("{$key}created", ['url' => $route]); 49 | 50 | return $this->with('success', $message); 51 | } 52 | ); 53 | 54 | RedirectResponse::macro( 55 | 'updated', 56 | function ($message = null) use ($key) { 57 | return $this->with( 58 | 'success', 59 | $message ?? trans("{$key}updated") 60 | ); 61 | } 62 | ); 63 | 64 | RedirectResponse::macro( 65 | 'deleted', 66 | function ($message = null) use ($key) { 67 | return $this->with('success', $message ?? trans("{$key}deleted")); 68 | } 69 | ); 70 | 71 | RedirectResponse::macro( 72 | 'error', 73 | function ($message) { 74 | return $this->with( 75 | 'error', 76 | $message = $this->determineMessage($message) 77 | ); 78 | } 79 | ); 80 | 81 | RedirectResponse::macro( 82 | 'errorNotFound', 83 | function ($message = null) use ($key) { 84 | $message = $this->determineMessage($message); 85 | 86 | return $this->with('error', $message ?? trans("{$key}not-found")); 87 | } 88 | ); 89 | 90 | RedirectResponse::macro( 91 | 'authorized', 92 | function ($message = null) use ($key) { 93 | return $this->with('success', $message ?? trans("{$key}authorized")); 94 | } 95 | ); 96 | 97 | RedirectResponse::macro( 98 | 'unAuthorized', 99 | function ($message = null) use ($key) { 100 | $message = $this->determineMessage($message); 101 | 102 | return $this->with('error', $message ?? trans("{$key}un-authorized")); 103 | } 104 | ); 105 | } 106 | 107 | public function register() 108 | { 109 | // 110 | } 111 | 112 | private function registerHelper() 113 | { 114 | RedirectResponse::macro( 115 | 'determineMessage', 116 | function ($message = null) { 117 | return $message instanceof Exception 118 | ? $message->getMessage() 119 | : ($message ?? null); 120 | } 121 | ); 122 | } 123 | } 124 | --------------------------------------------------------------------------------