0

我正在为 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'));;
        });
}
4

1 回答 1

0

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

您不必为此创建配置,该配置应该是捆绑的路由。给它一个名字就足够了,所以当你的包在多个项目中使用并且其中一些需要更改 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

于 2013-04-12T12:23:18.353 回答