我正在尝试为Laravel中的食谱应用程序创建一个页面视图计数器。我一直在将此SO帖子用作指南:Counting page views with Laravel
但是,当我尝试访问食谱时,出现了404错误。有人可以看一下是怎么回事吗?谢谢!
迁移
Schema::create('recipe_views', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger("recipe_id");
$table->string("titleslug");
$table->string("url");
$table->string("session_id");
$table->unsignedInteger('user_id')->nullable();
$table->string("ip");
$table->string("agent");
$table->timestamps();
});
模型
class RecipeView extends Model
{
public static function createViewLog($recipe) {
$recipeViews= new RecipeView();
$recipeViews->listing_id = $recipe->id;
$recipeViews->url = \Request::url();
$recipeViews->session_id = \Request::getSession()->getId();
$recipeViews->user_id = (\Auth::check())?\Auth::id():null;
$recipeViews->ip = \Request::getClientIp();
$recipeViews->agent = \Request::header('User-Agent');
$recipeViews->save();
}
}
收件人控制器
public function show($id)
{
$recipeView = RecipeView::where('id', '=' ,$id)->firstOrFail();
RecipeView::createViewLog($recipeView);
$recipe = Recipe::find($id);
$ingredients = explode("\n", $recipe->ingredients);
$directions = explode("\n", $recipe->directions);
return view('recipes.show')->with('recipe', $recipe)->with('directions', $directions)->with('ingredients', $ingredients);
}
路线
Route::resource('/recipes', 'RecipesController');
真的很感谢您的帮助。我对自己做错了事有些迷茫。谢谢!
答案 0 :(得分:2)
可以看到您的/index.php/recipes是否有效吗?
如果是,则需要按照以下步骤进行修复
$ sudo a2enmod重写
$ sudo nano /etc/apache2/apache2.conf
在此块中将AllowOverride从“无”更改为“全部”
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
$ sudo服务apache2重新启动
答案 1 :(得分:0)
您需要在路由中指定函数名称+变量。在您的情况下show()
resource('/recipes/{id}', 'RecipesController@show');