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条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)