laravel路由参数_了解Laravel路由参数

laravel路由参数

Today we'll be looking at more of Laravel's routing features. This time we'll be dealing with how Laravel handles route parameters. We've gone over Laravel routing before, but this time we'll be looking at more advanced scenarios.

今天,我们将研究Laravel的更多路由功能。 这次我们将讨论Laravel如何处理路线参数 。 之前我们已经讨论过Laravel路由 ,但是这次我们将研究更高级的场景。

路线参数 (Route Parameters)

Laravel let's us use route parameters in our routes. This helps when you want to create routes with things like a subcategory or a specific identifier (name, id, or any other parameter). Let's look at the different ways to use route parameters.

Laravel让我们在路线中使用路线参数。 当您想要创建带有子类别或特定标识符 (名称,ID或任何其他参数)之类的路由时,这会有所帮助。 让我们看看使用路由参数的不同方法。

获取基本路线参数 (Getting a Basic Route Parameter)

In this example, we will have a route for users and we'll pull the identifying parameter. We'll do it two different ways with name and with an id

在此示例中,我们将为用户提供一条路线,并拉出识别参数。 我们将使用nameid两种不同的方式

// get the cuteness level of a puppy
    Route::get('puppies/{cutelevel}', function($cutelevel) 
    {
        return 'This puppy is an absolute ' . $cutelevel . ' out of ' . $cutelevel;
    });

    // OR

    // get the parameter of name
    Route::get('users/{name}', function($name) 
    {
        return 'User Name is ' . $name;
    });

Testing Cuteness Level: Now in our browser, if we access http://example.com/puppies/5, our browser would show This puppy is an absolute 5 out of 5.

测试可爱度 :现在,在浏览器中,如果我们访问http://example.com/puppies/5 ,则浏览器将显示“ 这只小狗绝对是5之5”

Testing Name: Now in our browser, if we access http://example.com/users/chris, our browser would show User Name is Chris.

测试名称 :现在,在我们的浏览器中,如果我们访问http://example.com/users/chris ,则我们的浏览器将显示User Name是Chris

使用可选的路由参数 (Using Optional Route Parameters)

For this example, let's say we have a gallery of photos. We also have categories of photos. The category will be optional, otherwise, we'll just show all the photos.

对于此示例,假设我们有一个图库。 我们也有照片类别。 类别是可选的,否则,我们将仅显示所有照片。

// optional category
    Route::get('gallery/{category?}', function($category) 
    {
        // if category is set, show the category
        // if not, then show all
        if ($category)
            return 'This is the ' . $category . ' section.';
        else 
            return 'These are all the photos.';

    });

Testing Optional Category: If we visit http://example.com/gallery/puppies, our browser would return This is the puppies section.

测试可选类别 :如果我们访问http://example.com/gallery/puppies ,则浏览器将返回“ 这是小狗”部分。

Testing No Optional Category: If we visit http://example.com/gallery, our browser will return These are all the photos.

测试否可选类别 :如果我们访问http://example.com/gallery ,则我们的浏览器将返回这些都是照片。

路由参数默认值 (Route Parameter Defaults)

Let's say you always want a user to have a category selected. So if they don't have a category selected, they will automatically default to lets say the sunsets category.

假设您一直希望用户选择一个类别。 因此,如果未选择类别,则它们将自动默认为日落类别。

// optional category with a default
    Route::get('gallery/{category?}', function($category = 'sunsets')
    {
        return 'This is the ' . $category . ' category.';
    });

Testing No Category: If we visit http://example.com/gallery, then our browser will return This is the sunsets category.

测试无类别 :如果我们访问http://example.com/gallery ,则我们的浏览器将返回这是日落类别。

Testing A Category: If we visit http://example.com/gallery/puppies, then our browser will return This is the puppies category.

测试类别 :如果我们访问http://example.com/gallery/puppies ,则我们的浏览器将返回“ 这是小狗类别”。

提取真实数据 (Pulling Real Data)

So we've gone through the basics of route parameters. Let's talk about using these for a real world scenario. We will use the galleries example.

因此,我们已经研究了路由参数的基础。 让我们谈谈在现实世界中使用它们的情况。 我们将使用画廊示例

In the real world, you wouldn't want to give your visitor a sentence just telling them that they are seeing the puppies category. Your user will want to see puppies!.

在现实世界中,您不希望给访客一个句子,而只是告诉他们他们看到的是小狗类别。 您的用户将要看到小狗!

Let's say you already set up your Laravel application, migrations, and database. We can use Eloquent to pull data based on our route parameters.

假设您已经设置了Laravel应用程序,迁移和数据库。 我们可以使用Eloquent根据路线参数提取数据。

Once you also have your Eloquent model, you can call the information you need from the route (of course when your application gets larger, you'll want to move this logic into a controller).

一旦有了Eloquent模型,就可以从路由中调用所需的信息(当然,当您的应用程序变大时,您需要将此逻辑移至控制器中)。

// get the category of gallery for viewing
    Route::get('gallery/{category?}', function($category) {

        // get the gallery stuff for the category
        $gallery = Gallery::where('category', '=', $category);

        // return a view and send the gallery data to the view
        return View::make('gallery')
            ->with('gallery', $gallery);
    });

结论 (Conclusion)

As you can see, it is very easy to use route parameters and the Eloquent ORM to pull real data and send it to your view.

如您所见,使用路由参数和Eloquent ORM提取真实数据并将其发送到视图非常容易。

Let us know if there are any specific application use cases you'd like to see and we'll try to help out. You can expand on this with filters for authentication, route groups to prefix all routes, and much more.

让我们知道您是否想查看任何特定的应用程序用例,我们将尽力提供帮助。 您可以使用用于身份验证的过滤器,为所有路由添加前缀的路由组等扩展功能。

I'd advise you to check out the official Laravel route parameter docs to do more things like route constraints using regular expressions and more.

我建议您查看官方的Laravel 路线参数文档,以使用正则表达式执行更多操作,例如路线约束等。

翻译自: https://scotch.io/tutorials/understanding-laravel-route-parameters

laravel路由参数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值