我在 Laravel 5 中使用 Hashid 来隐藏资源的 ID。
这是路由文件中的路由绑定(bind):
Route::bind('schedule', function($value, $route)
{
$hashids = new Hashids\Hashids(env('APP_KEY'),8);
if( isset($hashids->decode($value)[0]) )
{
$id = $hashids->decode($value)[0];
return App\Schedule::findOrFail($id);
}
App::abort(404);
});
在模型中:
public function getRouteKey()
{
$hashids = new \Hashids\Hashids(env('APP_KEY'),8);
return $hashids->encode($this->getKey());
}
现在这工作正常,资源显示完美,ID 被散列。 但是当我转到我的创建路径时,它是 404 - 如果我删除 App::abort(404),创建路径将转到资源“显示” View ,没有任何数据...
这是创建路径:
Route::get('schedules/create', [
'uses' => 'SchedulesController@create',
'as' => 'schedules.create'
]);
演出路线:
Route::get('schedules/{schedule}', [
'uses' => 'Schedules Controller@show',
'as' => 'schedules.show'
]);
我还将模型绑定(bind)到路线:
Route::model('schedule', 'App\Schedule');
知道为什么我的创建 View 没有正确显示吗?索引 View 显示正常。
最佳答案
原来要解决这个问题,我不得不重新安排我的 crud 路线。
创建需要在 Show 路由之前...
关于php - Laravel 5 路由绑定(bind)和 Hashid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30992452/