I'm fairly new to laravel and I'm struggling to get the format of my url correct.
It formats as http://mysite/blog?category1
instead of http://mysite/blog/category1
These are the files I am using. Is there a way to put the route into the BlogController
?
Route.php
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category)->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
});
Blogcontroller
class BlogController extends BaseController {
public function index()
{
// get the posts from the database by asking the Active Record for "all"
$posts = Post::all();
// and create a view which we return - note dot syntax to go into folder
return View::make('blog.index', array('posts' => $posts));
}
}
blog.index blade
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
<h2>{{ HTML::link(
action('BlogController@index',array($post->category)),
$post->category)}}
@endforeach
routes.php
Route::get('category', 'CategoryController@indexExternal');
*.blade.php print the completed url
<a href="{{url('category/'.$category->id.'/subcategory')}}" class="btn btn-primary" >Ver más</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With