dtyqflrr775518 2015-05-17 21:29 采纳率: 100%
浏览 51
已采纳

Laravel自定义路线模型绑定

I have the following setup:

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

Page.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'];

}

URLS look like such:

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

Retrieving pages works fine; but my question is in regards to generating the URL

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

Simply Generates, localhost/page-id

I know I can do:

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

But I would much rather do this cleaner if possible. Does anyone have any recommendations?

  • 写回答

2条回答 默认 最新

  • ds2321 2015-05-17 21:44
    关注

    As you say, you can do {{URL::route('page',['page'=>$page->generateURLString()])}} because route('page',$page) will return the patter name.

    Then, my advice is, as you need some cleaner, to create a custom function extending the Route class or just declare it as a conventional function:

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

    Then just do:

    {{ page($page->generateURLString()) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?