【问题标题】:Laravel Custom Route Model BindingLaravel 自定义路由模型绑定
【发布时间】:2016-09-13 15:24:00
【问题描述】:

我有以下设置:

routes.php

Route::get('{page?}', [
    'uses'=>'PageController@getPage',
    'as'=>'page'
])->where('page', '(.*)?');

RouteServiceProvider.php

$router->bind('page', function($value, $route)
{
    if($value == "/"){ $value = "home"; };
    $explodedPage = explode("/",$value);
    $page = Page::findBySlug(last($explodedPage));
    if(!isset($page)){
        \App::abort(404);
    }
    $ancestors = $page->ancestorsAndSelf()->get();
    $sections=array();
    foreach($ancestors as $ancestor)
    {
        $sections[]=$ancestor->slug;
    }
    if(implode("/",$sections)==$value){
        return $page;
    }else{
        return $page;
        //Else Redirect
    }
});

页面.php

use Baum\Node;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use URL;
use Venturecraft\Revisionable\RevisionableTrait;
use Illuminate\Database\Eloquent\SoftDeletes;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;

class Page extends Node implements SluggableInterface
{

    use RevisionableTrait, SoftDeletes, SluggableTrait;

    protected $sluggable = array(
        'build_from' => 'title',
        'save_to'    => 'slug',
    );

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['title', 'description', 'content', 'owner_id', 'system', 'status'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['parent_id','lft','rgt','depth'];

    /**
     * The attributes excluded from revision
     *
     * @var array
     */
    protected $dontKeepRevisionOf = ['updater_id','parent_id','lft','rgt','depth'];

}

网址看起来像这样:

localhost/ (uses pre-defined slug)  
localhost/page-slug  
localhost/parent-slug/page-slug  
localhost/parent-parent-slug/parent-slug/page-slug  
Etc...  

检索页面工作正常;但我的问题是关于生成 URL

{{URL::route('page',$page)}}

简单地生成, 本地主机/页面 ID

我知道我能做到:

{{URL::route('page',['page'=>$page->generateURLString()])}}

但如果可能的话,我宁愿做这个更清洁。有人有什么建议吗?

【问题讨论】:

    标签: php laravel-5 laravel-routing


    【解决方案1】:

    正如您所说,您可以使用{{URL::route('page',['page'=>$page->generateURLString()])}},因为route('page',$page) 将返回模式名称。

    然后,我的建议是,因为您需要一些更清洁的东西,所以创建一个扩展 Route 类的自定义函数或将其声明为常规函数:

    public function page($bind){
        return route('page', ['page' => $bind]);
    }
    

    那就这样吧:

    {{ page($page->generateURLString()) }}
    

    【讨论】:

      【解决方案2】:

      从版本 5 开始,您现在可以在您的 Model(通过 UrlRoutable)上添加一个 getRouteKey(),您可以使用它来返回您的自定义路由密钥。 比如:

      class Page extends Node implements SluggableInterface
      {
          //......
      
          public function getRouteKey() {
              return $this->generateURLString();
          }
      }
      

      {{ route('page', $page) }} 表现得如你所愿。

      Docs

      【讨论】:

        猜你喜欢
        • 2021-05-14
        • 2021-05-24
        • 1970-01-01
        • 2016-06-04
        • 2018-06-12
        • 2017-06-22
        • 2022-01-21
        • 2018-12-17
        • 2015-07-12
        相关资源
        最近更新 更多