├── .gitignore ├── tests ├── stubs │ └── views │ │ └── contact.blade.php ├── TestCase.php ├── ViewControllerTest.php └── RedirectControllerTestCase.php ├── .travis.yml ├── src ├── ServiceProvider.php ├── ViewController.php ├── RedirectController.php └── macros.php ├── phpunit.xml.dist ├── LICENSE.md ├── composer.json └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | vendor/ 3 | composer.lock 4 | -------------------------------------------------------------------------------- /tests/stubs/views/contact.blade.php: -------------------------------------------------------------------------------- 1 | Contact us 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.6 5 | - 7.0 6 | - 7.1 7 | 8 | sudo: false 9 | 10 | install: travis_retry composer install --no-interaction --prefer-dist 11 | 12 | script: vendor/bin/phpunit --verbose 13 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | route('view')); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | set('view.paths', [__DIR__.'/stubs/views']); 10 | 11 | return [\Brayniverse\RouteMacros\ServiceProvider::class]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/RedirectController.php: -------------------------------------------------------------------------------- 1 | route('destination'), $request->route('status')); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/macros.php: -------------------------------------------------------------------------------- 1 | defaults('view', $view); 9 | }); 10 | } 11 | 12 | if (! Route::hasMacro('redirect')) { 13 | Route::macro('redirect', function ($url, $destination, $status = 301) { 14 | return Route::any($url, '\Brayniverse\RouteMacros\RedirectController@handle') 15 | ->defaults('destination', $destination) 16 | ->defaults('status', $status); 17 | }); 18 | } 19 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /tests/ViewControllerTest.php: -------------------------------------------------------------------------------- 1 | get('/contact'); 14 | 15 | // In order to support versions of Laravel below v5.4 16 | // you need to check if the `assertSee` method exists 17 | // as `see` was renamed to `assertSee` in the update. 18 | if (method_exists($response, 'assertSee')) { 19 | $response->assertSee('Contact us'); 20 | } else { 21 | $response->see('Contact us'); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /tests/RedirectControllerTestCase.php: -------------------------------------------------------------------------------- 1 | get('/contact_us'); 15 | 16 | if (method_exists($response, 'assertRedirect')) { 17 | $response->assertRedirect('/contact'); 18 | } else { 19 | $this->assertRedirectedTo('/contact'); 20 | } 21 | } 22 | 23 | public function test_can_override_default_redirect_status() 24 | { 25 | Route::redirect('/contact_us', '/contact', 302); 26 | Route::get('/contact', function () {}); 27 | 28 | $response = $this->get('contact_us'); 29 | 30 | if (method_exists($response, 'assertStatus')) { 31 | $response->assertStatus(302); 32 | } else { 33 | $this->assertResponseStatus(302); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Christopher L Bray 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "brayniverse/laravel-route-macros", 3 | "description": "A collection of useful route macros.", 4 | "keywords": [ 5 | "laravel", 6 | "router", 7 | "macros" 8 | ], 9 | "homepage": "https://github.com/brayniverse/laravel-route-macros", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Christopher L Bray", 14 | "email": "chris@brayniverse.com", 15 | "homepage": "http://brayniverse.com" 16 | } 17 | ], 18 | "require": { 19 | "php": "^5.5.9 || ^7.0", 20 | "illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.*", 21 | "illuminate/http": "5.1.* || 5.2.* || 5.3.* || 5.4.*" 22 | }, 23 | "require-dev": { 24 | "phpunit/phpunit": "^4.8 || ^5.0", 25 | "orchestra/testbench": "~3.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "Brayniverse\\RouteMacros\\": "src/" 30 | } 31 | }, 32 | "autoload-dev": { 33 | "psr-4": { 34 | "Brayniverse\\RouteMacros\\Tests\\": "tests/" 35 | } 36 | }, 37 | "scripts": { 38 | "test": "vendor/bin/phpunit" 39 | }, 40 | "config": { 41 | "sort-packages": true 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel route macros 2 | 3 | [![Build Status](https://travis-ci.org/brayniverse/laravel-route-macros.svg)](https://travis-ci.org/brayniverse/laravel-route-macros) 4 | [![Total Downloads](https://poser.pugx.org/brayniverse/laravel-route-macros/d/total.svg)](https://packagist.org/packages/brayniverse/laravel-route-macros) 5 | [![Latest Stable Version](https://poser.pugx.org/brayniverse/laravel-route-macros/v/stable.svg)](https://packagist.org/packages/brayniverse/laravel-route-macros) 6 | [![Latest Unstable Version](https://poser.pugx.org/brayniverse/laravel-route-macros/v/unstable.svg)](https://packagist.org/packages/brayniverse/laravel-route-macros) 7 | [![License](https://poser.pugx.org/brayniverse/laravel-route-macros/license.svg)](https://packagist.org/packages/brayniverse/laravel-route-macros) 8 | 9 | ## Installation 10 | 11 | Begin by installing the package through Composer. 12 | 13 | ```bash 14 | $ composer require brayniverse/laravel-route-macros 15 | ``` 16 | 17 | Then add the following to your providers array in `config/app.php`. 18 | 19 | ```php 20 | Brayniverse\RouteMacros\ServiceProvider::class 21 | ``` 22 | 23 | ## Usage 24 | 25 | ### `view` 26 | 27 | Normally you'd have to return a view in either a controller method or callback like the following: 28 | 29 | ```php 30 | public function contact() 31 | { 32 | return view('contact'); 33 | } 34 | 35 | // or 36 | 37 | Route::get('/contact', function () { 38 | return view('contact'); 39 | }); 40 | ``` 41 | 42 | Now you can do the same in one line. 43 | 44 | ```php 45 | Route::view('/contact', 'contact'); 46 | ``` 47 | 48 | ### `redirect` 49 | 50 | Normally you'd have to create a closure to redirect to the new route. 51 | 52 | ```php 53 | Route::get('/contact_us', function () { 54 | return redirect('/contact'); 55 | }); 56 | ``` 57 | 58 | Now you can do the same in one line. 59 | 60 | ```php 61 | Route::redirect('/contact_us', '/contact'); 62 | ``` 63 | 64 | Optionally, you can pass a third argument to `Route::redirect()` which will set the status code when redirecting. If you do not specify a status code, the package will use `301` as the status code. 65 | 66 | ```php 67 | Route::redirect('/contact_us', '/contact', 302); 68 | ``` 69 | ## Credits 70 | 71 | - [Christopher L Bray](https://github.com/brayniverse) 72 | - [All Contributors](../../contributors) 73 | --------------------------------------------------------------------------------