├── .gitattributes ├── .gitignore ├── CHANGELOG.md ├── .travis.yml ├── src ├── Facades │ └── MultiKyeRouteBinding.php ├── MultiKyeRouteBindingServiceProvider.php └── HasMultipleRouteBindingKeys.php ├── tests ├── App │ ├── Profile.php │ ├── User.php │ └── database │ │ └── migrations │ │ ├── 2014_10_12_000001_create_profiles_table.php │ │ └── 2014_10_12_000000_create_users_table.php └── LaravelIntegrationTest.php ├── LICENSE.md ├── composer.json ├── phpunit.xml.dist └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /vendor 3 | /build 4 | composer.lock 5 | .phpunit.result.cache -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | ## [Unreleased] 5 | 6 | 7 | ## [1.0.0] - 2021-08-16 8 | - Initial release 9 | 10 | ## [1.0.1] - 2021-10-29 11 | - Bug fix. -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.3 5 | - 7.4 6 | - 8.0 7 | 8 | env: 9 | matrix: 10 | - COMPOSER_FLAGS="--prefer-lowest" 11 | - COMPOSER_FLAGS="" 12 | 13 | before_script: 14 | - travis_retry composer update ${COMPOSER_FLAGS} 15 | 16 | script: 17 | - vendor/bin/phpunit -------------------------------------------------------------------------------- /src/Facades/MultiKyeRouteBinding.php: -------------------------------------------------------------------------------- 1 | getSchemaBuilder()->create('profiles', function (Blueprint $table) { 16 | $table->increments('id'); 17 | $table->string('employee_number')->nullable(); 18 | $table->string('first_name'); 19 | $table->string('last_name'); 20 | $table->timestamps(); 21 | $table->softDeletes(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | DB::connection()->getSchemaBuilder()->dropIfExists('profiles'); 33 | } 34 | } -------------------------------------------------------------------------------- /tests/App/database/migrations/2014_10_12_000000_create_users_table.php: -------------------------------------------------------------------------------- 1 | getSchemaBuilder()->create('users', function (Blueprint $table) { 16 | $table->increments('id'); 17 | $table->string('username')->nullable()->unique()->index(); 18 | $table->string('email')->unique(); 19 | $table->string('password', 60); 20 | $table->timestamps(); 21 | $table->softDeletes(); 22 | }); 23 | } 24 | 25 | /** 26 | * Reverse the migrations. 27 | * 28 | * @return void 29 | */ 30 | public function down() 31 | { 32 | DB::connection()->getSchemaBuilder()->dropIfExists('users'); 33 | } 34 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Touhidur Rahman Abir 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. -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "touhidurabir/laravel-multi-key-route-binding", 3 | "description": "A simple laravel package to handle multiple key based model route binding", 4 | "type": "library", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Touhidur Rahman", 9 | "email": "abircse06@gmail.com" 10 | } 11 | ], 12 | "autoload" : { 13 | "psr-4" : { 14 | "Touhidurabir\\MultiKyeRouteBinding\\": "src/" 15 | } 16 | }, 17 | "autoload-dev" : { 18 | "psr-4" : { 19 | "Touhidurabir\\MultiKyeRouteBinding\\Tests\\": "tests/" 20 | } 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "^9.5", 24 | "orchestra/testbench": "^6.20", 25 | "illuminate/support": "^8.54" 26 | }, 27 | "extra": { 28 | "laravel": { 29 | "providers": [ 30 | "Touhidurabir\\MultiKyeRouteBinding\\MultiKyeRouteBindingServiceProvider" 31 | ], 32 | "aliases": { 33 | "MultiKyeRouteBinding": "Touhidurabir\\MultiKyeRouteBinding\\Facades\\MultiKyeRouteBinding" 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Multi Key Route Binding 2 | 3 | A simple package to handle the multiple key/column based route model binding for laravel package 4 | 5 | ## Installation 6 | 7 | Require the package using composer: 8 | 9 | ```bash 10 | composer require touhidurabir/laravel-multi-key-route-binding 11 | ``` 12 | 13 | 14 | ## Usage 15 | 16 | Use the trait **HasMultipleRouteBindingKeys** in model where uuid needed to attach 17 | 18 | ```php 19 | use Touhidurabir\MultiKyeRouteBinding\HasMultipleRouteBindingKeys; 20 | use Illuminate\Database\Eloquent\Model; 21 | 22 | class User extends Model { 23 | 24 | use HasMultipleRouteBindingKeys; 25 | } 26 | ``` 27 | 28 | And then add the protected property **$routeBindingKeys** which will contails the list of other route binding keys. 29 | 30 | ```php 31 | /** 32 | * The attributes that will be used for multiple key binding on route models 33 | * 34 | * @var array 35 | */ 36 | protected $routeBindingKeys = [ 37 | 'email', 38 | 'username' 39 | ]; 40 | ``` 41 | 42 | By default column **id** is considered as the primary key for route model binding . But it is always possible to override/customize the primary key for route binding [as per laravel doc](https://laravel.com/docs/8.x/routing#customizing-the-default-key-name) 43 | 44 | ```php 45 | /** 46 | * Get the route key for the model. 47 | * 48 | * @return string 49 | */ 50 | public function getRouteKeyName() 51 | { 52 | return 'slug'; 53 | } 54 | ``` 55 | 56 | > The package also provide a bit of safe guard by checking if the model table has the given binding key column . 57 | > 58 | > If the binding key column not found for model table schema, it will skip that binding key. 59 | 60 | ## Contributing 61 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 62 | 63 | Please make sure to update tests as appropriate. 64 | 65 | ## License 66 | [MIT](./LICENSE.md) 67 | -------------------------------------------------------------------------------- /src/HasMultipleRouteBindingKeys.php: -------------------------------------------------------------------------------- 1 | routeBindingKeys) 42 | ? $this->routeBindingKeys 43 | : []; 44 | } 45 | 46 | 47 | /** 48 | * Get the route key for the model. 49 | * 50 | * @return string 51 | */ 52 | public function getRouteKeyName() { 53 | 54 | return 'id'; 55 | } 56 | 57 | 58 | /** 59 | * Retrieve the model for a bound value. 60 | * 61 | * @param mixed $value 62 | * @param string|null $field 63 | * 64 | * @return \Illuminate\Database\Eloquent\Model|null 65 | */ 66 | public function resolveRouteBinding($value, $field = null) { 67 | 68 | if ( $field && !is_null($field) ) { 69 | 70 | return $this->newQuery()->where($field, $value)->first(); 71 | } 72 | 73 | $bindingKeys = $this->getBindingKeys(); 74 | 75 | $query = $this->newQuery()->where($this->getRouteKeyName(), $value); 76 | 77 | $modelTableName = $this->getTable(); 78 | 79 | foreach ($bindingKeys as $bindingKey) { 80 | 81 | if ( Schema::hasColumn($modelTableName, $bindingKey) ) { 82 | $query = $query->orWhere($bindingKey, $value); 83 | } 84 | } 85 | 86 | return $query->first(); 87 | } 88 | } -------------------------------------------------------------------------------- /tests/LaravelIntegrationTest.php: -------------------------------------------------------------------------------- 1 | MultiKyeRouteBinding::class, 42 | ]; 43 | } 44 | 45 | 46 | /** 47 | * Define environment setup. 48 | * 49 | * @param Illuminate\Foundation\Application $app 50 | * @return void 51 | */ 52 | protected function defineEnvironment($app) { 53 | 54 | // Setup default database to use sqlite :memory: 55 | $app['config']->set('database.default', 'testbench'); 56 | $app['config']->set('database.connections.testbench', [ 57 | 'driver' => 'sqlite', 58 | 'database' => ':memory:', 59 | 'prefix' => '', 60 | ]); 61 | 62 | $app['config']->set('app.url', 'http://localhost/'); 63 | $app['config']->set('app.debug', false); 64 | $app['config']->set('app.key', env('APP_KEY', '1234567890123456')); 65 | $app['config']->set('app.cipher', 'AES-128-CBC'); 66 | } 67 | 68 | 69 | /** 70 | * Define database migrations. 71 | * 72 | * @return void 73 | */ 74 | protected function defineDatabaseMigrations() { 75 | 76 | $this->loadMigrationsFrom(__DIR__ . '/App/database/migrations'); 77 | 78 | $this->artisan('migrate', ['--database' => 'testbench'])->run(); 79 | 80 | $this->beforeApplicationDestroyed(function () { 81 | $this->artisan('migrate:rollback', ['--database' => 'testbench'])->run(); 82 | }); 83 | } 84 | 85 | 86 | /** 87 | * Define routes setup. 88 | * 89 | * @param \Illuminate\Routing\Router $router 90 | * 91 | * @return void 92 | */ 93 | protected function defineRoutes($router) { 94 | 95 | Route::get('/users/{user}', function(User $user) { 96 | 97 | if ( $user ) { 98 | 99 | return response()->json([ 100 | 'id' => $user->id, 101 | 'message' => 'resource found' 102 | ], 200); 103 | } 104 | 105 | return response()->json([ 106 | 'message' => 'resource not found' 107 | ], 404); 108 | 109 | })->middleware('bindings'); 110 | } 111 | 112 | 113 | /** 114 | * @test 115 | */ 116 | public function the_test_route_return_200_if_model_resource_found_using_default_key_binding() { 117 | 118 | $user = User::create([ 119 | 'email' => uniqid() . '@localhost.com', 120 | 'password' => bcrypt('password'), 121 | ]); 122 | 123 | $this->get("/users/{$user->id}")->assertStatus(200); 124 | } 125 | 126 | 127 | /** 128 | * @test 129 | */ 130 | public function the_test_route_return_404_if_model_resource_not_found_using_default_key_binding() { 131 | 132 | $user = User::create([ 133 | 'email' => uniqid() . '@localhost.com', 134 | 'password' => bcrypt('password'), 135 | ]); 136 | 137 | $this->get("/users/10001")->assertStatus(404); 138 | } 139 | 140 | 141 | /** 142 | * @test 143 | */ 144 | public function it_find_model_resource_on_keybind() { 145 | 146 | $user1 = User::create([ 147 | 'email' => uniqid() . '@localhost.com', 148 | 'password' => bcrypt('password'), 149 | ]); 150 | 151 | $user2 = User::create([ 152 | 'email' => uniqid() . '@localhost.com', 153 | 'username' => uniqid() . '_username', 154 | 'password' => bcrypt('password'), 155 | ]); 156 | 157 | $this->get("/users/{$user1->email}")->assertStatus(200); 158 | $this->get("/users/{$user2->username}")->assertStatus(200); 159 | } 160 | 161 | 162 | /** 163 | * @test 164 | */ 165 | public function it_can_not_find_model_resource_on_keybind_if_no_match_found() { 166 | 167 | $user1 = User::create([ 168 | 'email' => uniqid() . '@localhost.com', 169 | 'password' => bcrypt('password'), 170 | ]); 171 | 172 | $this->get("/users/someemail@email.com")->assertStatus(404); 173 | $this->get("/users/someusername")->assertStatus(404); 174 | } 175 | } --------------------------------------------------------------------------------