【问题标题】:dynamic laravel4 service provider routes动态 laravel4 服务提供者路由
【发布时间】:2013-04-03 10:32:17
【问题描述】:

我正在为 Laravel4 准备一个捆绑包,但找不到向我的捆绑包添加可配置路由的方法。

例子:

//this is from the service provider of the bundle
public function register()
{
    ...
    $this->registerRoutes();
    ...
}

protected function registerRoutes()
{
     //The way of doing into the documentation or other bundles is
     //the problem is that I cannot put the routes to be from the config file
     //and they cannot be overwritten from the app configuration.
     include __DIR__ . '/routes.php';
}
//This method gives an error
protected function registerTestRoutes()
{
    $this->app['imager'] = $this->app->share(function($app)
        {
            //$route = 'admin/images/{$image_id}
            $route = $app['config']['imager::config']['delete_url'];
            return \Route::delete($route, array('uses' => 'CompanyName\Imager\Controllers\ImagerController@destroy'));;
        });
}

【问题讨论】:

  • 请你详细说明你得到的错误,只是说它给出了一个错误并不能帮助任何人与你一起调试。

标签: php routing laravel service-provider


【解决方案1】:

我找到了问题的解决方案。

您不必为此创建配置,该配置应该是捆绑的路由。给它一个名字就足够了,所以当你的包在多个项目中使用并且其中一些需要更改 url 时,那里的开发人员可以简单地放一个别名。

// code from the SerivicePriveder
public function register()
{
...
$this->registerRoutes();
...
}

protected function registerRoutes()
{
 include __DIR__ . '/routes.php';
}

//code from the routes.php
<?php
Route::delete('admin/imager/{id}', array('as'=>'imager_delete', 'uses' => 'SixMinutes\Imager\Controllers\ImagerController@destroy'));

因此,如果需要更改“admin/imager/{id}”,您可以使用名称“imager_delete”在 app/routes.php 中添加一个别名

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 2015-04-16
    • 2016-08-05
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    • 2017-07-26
    相关资源
    最近更新 更多