Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5路由绑定和Hashid_Php_Routes_Laravel 5_Hashids - Fatal编程技术网

Php Laravel 5路由绑定和Hashid

Php Laravel 5路由绑定和Hashid,php,routes,laravel-5,hashids,Php,Routes,Laravel 5,Hashids,我正在使用Hashid隐藏Laravel5中资源的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];

我正在使用Hashid隐藏Laravel5中资源的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),那么创建路由将转到资源“显示”视图,而没有任何数据

以下是创建路线:

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');

你知道为什么我的create视图显示不正确吗?索引视图显示良好。

为了解决这个问题,我不得不重新安排crud路径


在放映路线之前创建所需的内容…

有一个软件包,它完全满足您的要求:

还要注意的是,使用
APP_KEY
作为salt不是一个好主意,因为

使用上述软件包,您只需在控制器中添加一个trait和typehint:

类后扩展模型{
使用HasHashSlug;
}
//routes/web.php
路由::资源('/posts',PostController');
//app/Http/Controllers/PostController.php
公共活动展览(Post$Post){
返回视图('post.show',压缩视图('post');
}