I'm setting up a "slug history" system.
For example, if I had an article with the slug my-article but then edited the slug later to be my-new-article, I want my-article to redirect to my-new-article and still work.
The URL contains no ID, so it's just based on a unique slug.
I have a database table called slugs which stores all the slugs, new and old, attached to a specific article ID.
id | slug | article_id
private function oldSlug()
{
$oldslug = DB::table('slugs')
->where('slug', $this->article_slug)
->first();
if ($oldslug == null) {
abort(404);
} else {
/*
There is an article with that slug. Get the newest slug from the ID,
and set the object property.
*/
$article = Article::find($oldslug->article_id);
$this->article_slug = $article->slug;
$this->article_id = $article->id;
// Now, I want to use the `show()` method again with the new info
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($category_slug, $article_slug)
{
$this->category_slug = $category_slug;
$this->article_slug = $article_slug;
$article = Article::selectRaw('*, `articles`.`id` as article_id, `categories`.`name` as category_name, `categories`.`slug` as category_slug, `articles`.`slug` as article_slug')
->join('categories', 'categories.id', '=', 'articles.category_id')
->where(['articles.slug' => $article_slug, 'categories.slug' => $category_slug])
->first();
$article == null ? $this->oldSlug() : $this->article_id = $article->article_id;
$this->addView();
return view('article')->withArticle($article);
}
In the show() method, if the $article result is null, then it will go to the oldSlug() method to check any potential old slugs. If there's no old slugs, it just shows a 404, otherwise, it should show the article with the updated slug.
Problem is, redirects aren't working, nor calling the show method again. New to Laravel, any help would be great.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/20BMcJu
via IFTTT
Aucun commentaire:
Enregistrer un commentaire