[英]Laravel 5 Route binding and Hashid
我正在使用Hashid隱藏Laravel 5中的資源ID。
這是路由文件中的路由綁定:
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),創建路線將進入資源'show'視圖而沒有任何數據......
這是創建路線:
Route::get('schedules/create', [
'uses' => 'SchedulesController@create',
'as' => 'schedules.create'
]);
顯示路線:
Route::get('schedules/{schedule}', [
'uses' => 'Schedules Controller@show',
'as' => 'schedules.show'
]);
我也將模型綁定到路線:
Route::model('schedule', 'App\Schedule');
我的創建視圖無法正確顯示的任何想法? 索引視圖顯示正常。
原來要解決這個問題,我不得不重新安排我的粗線路。
在顯示路線之前需要創建...
有一個包完全符合你的要求: https : //github.com/balping/laravel-hashslug
另請注意,使用APP_KEY
作為鹽並不是一個好主意,因為它可以暴露 。
使用上面的包你需要做的就是在控制器中添加一個特征和類型提示:
class Post extends Model {
use HasHashSlug;
}
// routes/web.php
Route::resource('/posts', 'PostController');
// app/Http/Controllers/PostController.php
public function show(Post $post){
return view('post.show', compact('post'));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.