Passing a variable through url in laravel
51,456
Solution 1
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>
Solution 2
I have added a new route in:
Route::get('blog/{category}', ['as' => 'post.path', 'uses' => 'BlogController@getCategory']);
and added a new link into index.blade:
<a href="{{ URL::route('post.path', [$post->category]) }}">{{ $post->category }}</a>
Related videos on Youtube
Author by
tom harrison
Updated on July 09, 2022Comments
-
tom harrison almost 2 years
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 ofhttp://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
-
Martijn Thomas over 9 yearsAre you on apache or nginx, I think this is a rewrite problem of the url.
-
Tom Macdonald over 9 yearsWhat do you mean by "It formats as" ? When you type into the browser? Or the links generated by Laravel?
-
tom harrison over 9 yearsThe link generated by laravel from the db. it now display as localhost/blog?category=category1 and it also doesn't filter the db results so something is wrong somewhere
-
-
tom harrison over 9 yearsThanks, I have updated my code but it still displays the link with the ? instead of the /.
-
tom harrison over 9 yearsThanks, I have updated my code with the above and it still displays the link as mysite/blog?category1 instead of mysite/blog/category1
-
tom harrison over 9 yearsI have just tried this and no luck it still displays in the same way
-
Adrenaxus over 9 yearsCan you try to enter some junk value into your
.htaccess
to verify if it works? If it works you should get a500 Internal Server Error
-
Adrenaxus over 9 yearsWell at least the .htaccess is working. We need more information such as whether you are on apache or nginx and what server environment you are working on (LAMP, WAMP,...) - also: is mod_rewrite enabled on your server?
-
tom harrison over 9 yearsmod_rewrite is enabled and i am running on xampp. Just to be clear if i got to mysite/blog/category1 manually, it displays the correct result. However it still outputs the url as mysite/blog?category1 using the laravel HTML:Link
-
Peter over 8 yearsin my app in my url i place ?category=1 and then in my controller i extract the variable with category = Input::get('category');