Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
#24 Handle model binding routes in if_route_param
Browse files Browse the repository at this point in the history
  • Loading branch information
letrunghieu committed Jul 23, 2016
1 parent ce868e1 commit f2936d4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/Active.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace HieuLe\Active;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -250,7 +251,15 @@ public function checkRouteParam($param, $value)
return false;
}

return $this->route->parameter($param) == $value;
$paramValue = $this->route->parameter($param);

// If the parameter value is an instance of Model class, we compare $value with the value of
// its primary key.
if (is_a($paramValue, Model::class)) {
return $paramValue->{$paramValue->getKeyName()} == $value;
}

return $paramValue == $value;
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/ActiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public function setUp()
]);
app('router')->get('/', function () {
});
app('router')->bind('model', function ($id) {
return new StubModel(['uid' => $id]);
});
app('router')->get('/model/{model}', '\HieuLe\ActiveTest\Http\DumpController@viewMethod');
});
}

Expand Down Expand Up @@ -118,7 +122,8 @@ public function testCheckCurrentAction(Request $request, $actions, $result)

/**
* @param Request $request
* @param $controllers
* @param
* $controllers
* @param $result
*
* @dataProvider provideCheckControllerTestData
Expand Down Expand Up @@ -378,6 +383,18 @@ public function provideCheckRouteParameterTestData()
'2',
false,
],
'match a route bound to a model' => [
Request::create('/model/100'),
'model',
'100',
true,
],
'not match a route bound to another model' => [
Request::create('/model/100'),
'model',
'1',
false,
],
];
}

Expand Down
12 changes: 12 additions & 0 deletions tests/StubModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace HieuLe\ActiveTest;


use Illuminate\Database\Eloquent\Model;

class StubModel extends Model
{
protected $fillable = ['id', 'uid'];
protected $primaryKey = 'uid';
}

0 comments on commit f2936d4

Please sign in to comment.