I would like to be able to display different content depending on which variable is called in the url.Ignoring the fact $slug is slug, let's say it's a post id instead so if $id is active then show just the post else if $month is active show all posts from that month elseif $month and $id = null show all. That's what I'm trying to achive
Routes.php
Route::get('blog/{slug}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']);
Route::get('blog/{month}', ['as' => 'blog.slug', 'uses' => 'BlogController@getSlug']);
BlogController.php
<?php
class BlogController extends BaseController {
public function BlogIndex()
{
//get the posts from the database by asking the Active Record for "all"
$blogs = Blog::all();
$blogs = DB::table('blogs')->paginate(3);
// and create a view which we return - note dot syntax to go into folder
return View::make('pages.blog', array('blogs' => $blogs));
}
public function ShowbySlug($slug)
{
$blogs = Blog::where('slug', '=', $slug)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('slug', $slug)
->with('blogs', $blogs);
}
public function ShowbyMonth($month)
{
$blogs = Blog::where('month', '=', $month)->get();
// show the view with blog posts (app/views/pages/blog.blade.php)
return View::make('pages.blog')
->with('month', $month)
->with('blogs', $blogs);
}
}
blog.blade.php
@foreach ($blogs as $blog)
@if(isset($blogs))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
<a href="{{ URL::route('blog.slug', [$blog->slug]) }}">
</div>
@elseif(isset($slug))
<div class="blog-outer-wrap">
<img src="images/blog/{{ $blog->img}}">
<div class="blog-header">{{ $blog->header }}</div>
<div class="blog-text">{{ $blog->content }}</div>
</div>
@endif
@endforeach
You really can't do that, because you just declared the same route twice. When you say a route is blog/{slug}
, slug
is only a placeholder. It's exactly the same¹ as blog/{month}
. All it says is: "I expect blog/
followed by anything." It doesn't matter if you call your anything slug
or month
. That is, the parameters do not add up.
What you can do is, if you consider slug
is always a string and month
is always a number (or the name of the month²), is to apply where clauses to your route parameters, like so:
// Route for all blog posts
Route::get('blog', 'BlogController@showAll');
// Route for all blog posts in a month; only numbers as parameters
Route::get('blog/{month}', 'BlogController@showByMonth')
->where('month', '[0-9]+');
// Route for all blog posts by title; anything else as parameters
Route::get('blog/{slang}', 'BlogController@showBySlang');
And on your controller, define three methods, one for each route:
public function showAll() {
$blogs = Blog::all();
return View::make('pages.blog')
->with('blogs', $blogs);
}
public function showByMonth($month) {
$blogs = Blog::where('month', $month)
->get();
return View::make('pages.blog')
->with('blogs', $blogs);
}
public function showBySlug($slug) {
$blogs = Blog::where('slug', $slug)
->get();
return View::make('pages.blog')
->with('blogs', $blogs);
}
¹ Unless you're using some more advanced routing features, but that's not really the point here.
² It is doable with the where clause, but it would be ugly if you wanted to consider all upper/lower case combinations. E.g: ([Jj][Aa][Nn][Uu][Aa][Rr][Yy]|[Ff][Ee][Bb][Rr][Uu][Aa][Rr][Yy]|...)