承接 echosters/routable 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

echosters/routable

Composer 安装命令:

composer require echosters/routable

包简介

Laravel Eloquent Routable Models For Resources Models

README 文档

README

Routable is a Laravel package for dealing with resources models routes.

Installation

Using composer to install routable.

composer require echosters/routable

Configuration

in your config\app.php add in your providers

'providers' => [
    ...,
    Echosters\Routable\Providers\RoutableServiceProvider::class,
];

Preparing your model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Echosters\Routable\Routable;

class YourModel extends Model
{
    use Routable;
}

Usage

$user = User::find(1);
$user->getRoute('edit'); // the same of route('users.edit',$user->id)

Methods

getRoutes()

User::find(1)->getRoutes();
Array
[
  "edit" => "http://example.com/users/1/edit"
  "update" => "http://example.com/users/1"
  "show" => "http://example.com/users/1"
  "destroy" => "http://example.com/users/1"
  "store" => "http://example.com/users"
  "index" => "http://example.com/users"
]

getRoute($route)

User::find(1)->getRoute('show');
string
"http://example.com/users/1"

getResourceRoute($route)

User::getResourceRoute('index');
string
"http://example.com/users"

Parameters

you can pass any additional params to any route by passing the the params as array in the second parameter as following.

User::find(1)->getRoute('show',['highlight' => 'true']); // "http://example.com/users/1?highlight=true"
User::find(1)->getRoutes('show',['highlight' => 'false']); // "http://example.com/users/1?highlight=false"
User::getResourceRoute('index',['id' => '1']); // "http://example.com/users?id=1"

Notes

Name Convention

if you want to override this name convention you can add getRouteName method in your model.

getRouteName

    public function getRouteName()
    {
        return 'custom-users';
    }
    //Then 
    $user = User::find(1);
    $user->getRoute('edit'); //will generate 'http://example.com/en/custom-users/1'

Locale Fixed Paramaters

if you are using locale paramaters just add them in your model as following

getFixedParameter

    public function getFixedParameter()
    {
        return [
            'locale' => 'en',
        ];
    }
    //Then 
    $user = User::find(1);
    $user->getRoute('edit'); //will generate 'http://example.com/en/users/1'

Relations

if you have a relation like below App\Models\Post.php

    public function user()
    {
        return $this->belongsTo(User::class);
    }

if Your web look like this

 Route::resource('{user}/posts',PostsController::class);

So in Your App\Models\Post.php Model

    public function getFixedParameter()
    {
        return [
            'user' => $this->user->id,// Or $this->user,
        ];
    }
    //Then 
    $post = Post::find(1); // Known that $post->user->id = 22;
    $post->getRoute('show'); //will generate 'http://example.com/22/posts/1'

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

统计信息

  • 总下载量: 33
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-12-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固