2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
2018-01-15 14:02:13 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-19 15:01:28 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2017-12-19 16:00:42 +00:00
|
|
|
use App\Models\Article;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2018-01-15 14:02:13 +00:00
|
|
|
use Illuminate\View\View;
|
2016-05-19 15:01:28 +01:00
|
|
|
use Jonnybarnes\IndieWeb\Numbers;
|
|
|
|
|
|
|
|
class ArticlesController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Show all articles (with pagination).
|
|
|
|
*
|
2019-10-27 19:31:33 +00:00
|
|
|
* @param int $year
|
|
|
|
* @param int $month
|
|
|
|
* @return View
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function index(int $year = null, int $month = null): View
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
|
|
|
$articles = Article::where('published', '1')
|
2018-01-15 14:02:13 +00:00
|
|
|
->date($year, $month)
|
2017-02-15 15:21:14 +00:00
|
|
|
->orderBy('updated_at', 'desc')
|
|
|
|
->simplePaginate(5);
|
2016-05-19 15:01:28 +01:00
|
|
|
|
2017-02-15 18:34:47 +00:00
|
|
|
return view('articles.index', compact('articles'));
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a single article.
|
|
|
|
*
|
2019-10-27 19:31:33 +00:00
|
|
|
* @param int $year
|
|
|
|
* @param int $month
|
|
|
|
* @param string $slug
|
|
|
|
* @return RedirectResponse|View
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function show(int $year, int $month, string $slug)
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
2017-08-09 22:15:45 +01:00
|
|
|
$article = Article::where('titleurl', $slug)->firstOrFail();
|
2016-05-19 15:01:28 +01:00
|
|
|
if ($article->updated_at->year != $year || $article->updated_at->month != $month) {
|
2018-01-06 21:50:15 +00:00
|
|
|
return redirect('/blog/'
|
|
|
|
. $article->updated_at->year
|
|
|
|
. '/' . $article->updated_at->format('m')
|
2019-10-27 19:31:33 +00:00
|
|
|
. '/' . $slug);
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
2017-02-15 18:34:47 +00:00
|
|
|
return view('articles.show', compact('article'));
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We only have the ID, work out post title, year and month
|
|
|
|
* and redirect to it.
|
|
|
|
*
|
2019-10-27 19:31:33 +00:00
|
|
|
* @param int $idFromUrl
|
|
|
|
* @return RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
*/
|
2018-01-15 14:02:13 +00:00
|
|
|
public function onlyIdInUrl(int $idFromUrl): RedirectResponse
|
2016-05-19 15:01:28 +01:00
|
|
|
{
|
2018-01-15 14:02:13 +00:00
|
|
|
$realId = resolve(Numbers::class)->b60tonum($idFromUrl);
|
2016-05-19 15:01:28 +01:00
|
|
|
$article = Article::findOrFail($realId);
|
|
|
|
|
|
|
|
return redirect($article->link);
|
|
|
|
}
|
|
|
|
}
|