在laravel中通过url传递变量

tom*_*son 4 php url url-routing laravel laravel-4

我是laravel的新手,我正在努力让我的网址格式正确.

格式为

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);
});
Run Code Online (Sandbox Code Playgroud)

这些是我正在使用的文件,有没有办法将路由放入BlogController

Route.php

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

Blogcontroller

@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
Run Code Online (Sandbox Code Playgroud)

blog.index刀片

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);
});
Run Code Online (Sandbox Code Playgroud)

era*_*uan 6

routes.php文件

Route::get('category', 'CategoryController@indexExternal');
Run Code Online (Sandbox Code Playgroud)

*.blade.php打印完成的网址

<a href="{{url('category/'.$category->id.'/subcategory')}}" class="btn btn-primary" >Ver más</a>
Run Code Online (Sandbox Code Playgroud)


Jer*_*dev 0

不要使用函数作为回调,而是Route::get使用控制器和操作:

Route::get('blog/{category}', 'BlogController@getCategory');
Run Code Online (Sandbox Code Playgroud)

现在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));
    }

    /**
     *  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);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:

要在视图中显示链接,您应该使用HTML::linkAction而不是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
Run Code Online (Sandbox Code Playgroud)