2017-01-16 137 views
0

我在我的应用程序中创建网址友好的,但它不工作,应用程序给我一些与“ - ”有关的问题。 它给我的错误:网址友好的路线

ErrorException in PostController.php line 60: 
Trying to get property of non-object 

我理想中的网址是:

http://domain.com/CATEGORY-title-of-post-ID 

我的路线是:

Route::get('{category}-{title}-{id}', '[email protected]'); 

PostController的显示功能:

public function show($category,$title,$id) 
    { 
     $post = Post::find($id); 
     $user = Auth::user(); 

     $comments = Comment::where('post_id',$id) 
          ->where('approved',1) 
          ->get(); 




     return view('posts.show',compact('post','comments','user')); 
    } 

刀片查看:

<?php 
    $title_seo = str_slug($feature->title, '-'); 
?> 
<a href="{{url($feature->categories[0]->internal_name."-".$title_seo."-".$feature->id)}}" rel="bookmark"> 
...</a> 

回答

2

有一个名为Eloquent-Sluggable库,将为每个岗位独特的蛞蝓和正确的URL编码。

安装(从文档拍摄):

composer require cviebrock/eloquent-sluggable:^4.1 

然后,通过增加对服务提供商的条目更新config/app.php

'providers' => [ 
    // ... 
    Cviebrock\EloquentSluggable\ServiceProvider::class, 
]; 

最后,在命令行再次发布默认的配置文件:

php artisan vendor:publish --provider="Cviebrock\EloquentSluggable\ServiceProvider" 

用,Sluggable特征添加到模型:

use Cviebrock\EloquentSluggable\Sluggable; 

class Post extends Model 
{ 
    use Sluggable; 

    /** 
* Return the sluggable configuration array for this model. 
* 
* @return array 
*/ 
public function sluggable() 
{ 
    return [ 
     'slug' => [ 
      'source' => 'title' 
      ] 
     ]; 
    } 

} 

当您保存您的模型的实例,库会自动创建一个slu and并将其保存到模型表格的新创建的slug列中。因此,要访问您要使用的slu 012 $model->slug

为了达到您想要的slu,,而不是从默认设置的title创建它。您可以通过sluggable方法字段名数组的source属性,使用点标记来访问相关模型的属性,像这样:

public function sluggable() 
{ 
    return [ 
     'slug' => [ 
      'source' => ['category.name','title','id'] 
      ] 
     ]; 
    } 

} 
+0

您好感谢您的答复,我只是实现它,并且已经将其保存在我的数据库,但我有一个问题,我怎么会构建它在我的路线?它仍然给我错误。 – Pedro

+0

是否有原因需要使用'{category} - {title} - {id}'?我在想,因为你是用短划线分开的,而且可以用短划线来填充,所以不知道你的类别什么时候结束和标题开始了。如果你在默认例子中使用'title',你可以将它传递给你的控制器,并执行Posts :: where('slug',$ slug) - > first();'。如果你仍然想使用类别,最好是使用斜线('/')分开并在你的路线中反映出来 – TimothyBuktu

1

你为什么手动genering您的“友好网址”?

你有route助手功能,它可以根据给定的参数为你建立一个URL。

Route::get('{category}-{title}-{id}', [ 
    'as => 'post.show', 
    'uses' => '[email protected]' 
]); 

echo route('post.show', ['testing', 'title', 'id']); // http://domain.dev/testing-title-id 

这并不是实现SEO友好的URL,反正最好的办法。


在你的控制器,你总是用你的ID找到一个职位,这意味着类别和标题是完全没用的,确定需要提供给用户哪些资源。

你可以让你的生活更轻松做类似:

Route::get('{id}-{slug}', [ 
    'as => 'post.show', 
    'uses' => '[email protected]' 
]); 

echo route('post.show', ['id', 'slug']); // http://domain.dev/id-slug 

在你的模型创建生成您的文章蛞蝓的辅助函数:

class Post 
{ 
    [...] 

    public function slug() 
    { 
     return str_slug("{$this->category}-{$this->title}"); 
    } 
} 

然后,在您的控制器中,您需要检查用于访问该文章的slu is是否正确,因为你不希望谷歌索引错误的slu post。你本质上强迫一个URL以某种方式,并且你不会丢失索引点。

class PostController 
{ 
    [...] 

    public function show($id, $slug) 
    { 
     $post = Post::findOrFail($id); 
     $user = Auth::user(); 

     if ($post->slug() !== $slug) { 
      return redirect()->route('posts.show', ['id' => 1, 'slug' => $post->slug()]); 
     } 

     $comments = Comment::where('post_id', $id)->where('approved', 1)->get(); 
     return view('posts.show', compact('post', 'comments', 'user')); 
    } 
}