2017-01-16 196 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')); 
    } 
}