5

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
3
  • Are you on apache or nginx, I think this is a rewrite problem of the url. Commented Oct 16, 2014 at 14:00
  • What do you mean by "It formats as" ? When you type into the browser? Or the links generated by Laravel? Commented Oct 16, 2014 at 14:32
  • The 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 Commented Oct 16, 2014 at 15:01

4 Answers 4

6

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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> 

Comments

0

Instead of using a function as callback for your Route::get use a controller and an action:

Route::get('blog/{category}', 'BlogController@getCategory');

Now in your 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 of 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

3 Comments

Thanks, I have updated my code but it still displays the link with the ? instead of the /.
Thanks, I have updated my code with the above and it still displays the link as mysite/blog?category1 instead of mysite/blog/category1
in my app in my url i place ?category=1 and then in my controller i extract the variable with category = Input::get('category');
0

Have you tried using the alternative .htaccess as shown in the documentation? Here you go:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

You need to place it in the public folder of your application.

Here is the original .htaccess in case you don't have it for whatever 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>

4 Comments

I have just tried this and no luck it still displays in the same way
Can you try to enter some junk value into your .htaccess to verify if it works? If it works you should get a 500 Internal Server Error
Well 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?
mod_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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.