简体   繁体   中英

Laravel 4 - Call Controller from Routes closures

I'm newbie in Laravel 4, I just want to set config when runtime in Routes (closures) before calling a controller. But it not work.
Here is my routes :

Route::get('/test', array(function(){
    $config = [
        'config_url' => 'https://demo.com/'
    ];

    \Config::set('app.test', 'test');
    \Config::set('config',$config);
    echo "123";die();
}, 'uses' => 'UserController@estimate_time'));

It go to the Action estimate_time of UserController and not run the function.

From my experience, you cannot use the route like this, because function(){} is equal to 'use' . I suppose that is a either-or relationship between function(){} and ['use'=>'XXX'] .

Maybe you can set all those config to be a middleware and then use:

Route::get('/test', ['uses' => 'UserController@estimate_time']);

I think route file is not the best place to set config. You either do it in your middleware or in your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM