php 通过laravel中的url传递变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26406295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:53:19  来源:igfitidea点击:

Passing a variable through url in laravel

phpurllaravellaravel-4url-routing

提问by tom harrison

I'm fairly new to laravel and I'm struggling to get the format of my url correct.

我对 Laravel 还很陌生,我正在努力使我的 url 格式正确。

It formats as http://mysite/blog?category1instead of http://mysite/blog/category1

据格式,http://mysite/blog?category1而不是http://mysite/blog/category1

These are the files I am using. Is there a way to put the route into the BlogController?

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

Route.php

路由.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

blog.index 刀片

@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

回答by erajuan

routes.php

路由文件

Route::get('category', 'CategoryController@indexExternal');

*.blade.php print the completed url

*.blade.php 打印完成的 url

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

回答by tom harrison

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:

并在index.blade 中添加了一个新链接

<a href="{{ URL::route('post.path', [$post->category]) }}">{{ $post->category }}</a> 

回答by Jerodev

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

而不是使用函数作为您Route::get使用控制器和动作的回调:

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

Now in your BlogControlleryou can create your function.

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

Update:

更新:

To display your links in your view, you should use HTML::linkActioninstead of HTML::link:

要在您的视图中显示您的链接,您应该使用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

回答by Adrenaxus

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

您是否尝试过使用文档中所示的替代 .htaccess ?干得好:

Options +FollowSymLinks
RewriteEngine On

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

You need to place it in the publicfolder of your application.

您需要将其放在public应用程序的文件夹中。

Here is the original .htaccess in case you don't have it for whatever reason

这是原始的 .htaccess 以防万一你因为任何原因没有它

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ / [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>