Passing variable via url in laravel
I am new to laravel and am struggling to get my url formatted correctly.
It is formatted like http://mysite/blog?category1
insteadhttp://mysite/blog/category1
These are the files I am using. Is there a way to put the route in 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
source to share
Instead of using a function as a callback for yours, Route::get
use a controller and an action:
Route::get('blog/{category}', 'BlogController@getCategory');
Now in BlogController
you can create your function.
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));
}
/**
* Your new function.
*/
public function getCategory($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);
}
}
Update:
To display your links in your view, you should use HTML::linkAction
instead HTML::link
:
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
{{ HTML::linkAction('BlogController@index', "Linkname", array('category' => $post->category)) }}
@endforeach
source to share
Have you tried using the alternative .htaccess as shown in the documentation? Here you are:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
You need to put it in public
your application folder .
Here is the original .htaccess if you don't have it for any reason.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
source to share