dongna9185 2014-10-11 21:07
浏览 88
已采纳

不使用控制器的路由

Hi I would like to use a route that doesn't use a controller. To be clear what I want to achieve I will first show you my code of the view called index.blade.php:

<div class="form-group" id="work-bar"  >
    {{Form::open(array('route'=>'user.store.work', 'method'=>'post'))}}
    <div class="center-bar">
        <div class="row">
            <div class="col-md-12" id="column-make">
                {{Form::text('order_name', '',array('class' => 'form-control', 'id'=>'work-name'))}}
                <div id="BarDropDown" href="#" >
                    <select class="form-control" id="BarSelectDropDown" name="worktypes" >
                        <option  selected value="order" >Offerte</option>
                        <option value="project">Project</option>
                        <option value="task">Taak</option>
                    </select>
                </div>
                {{Form::hidden('route', Request::path() )}}
                {{Form::submit('Maken',  array('class' => 'btn btn-default', 'id' => 'work-submit'))}}
            </div>
        </div>
    </div>
    {{Form::close()}}
</div>

Okay that's just a form with a dropdownlist. Here is the code of my route called routes.php:

<?php
Route::group(array('prefix'=>'user', 'before'=>'auth'),function(){
    Route::resource('usergroups', 'UsergroupsController');
    Route::resource('orders', 'OrdersController');
    Route::resource('users', 'UsersController');
    Route::resource('tasks', 'TasksController');
    Route::resource('projects', 'ProjectsController');

    Route::post('works',array('as'=>'user.store.work'), function() {
        $action = 'store';
        if (Input::get('worktypes')=="project") {
            return App::make('ProjectsController')->$action();  
        }
        elseif(Input::get('worktypes')=="order") {
            return App::make('OrdersController')->$action();
        }
        else{
            return App::make('TasksController')->$action();
        }
    });
});

Route::group(array('prefix'=>'user'),function(){
    Route::post('login',array('as'=>'user.login.post', 'uses'=>'UserAuthController@postLogin'));
    Route::get('logout',array('as'=>'user.logout', 'uses'=>'UserAuthController@getLogout'));
});

Route::get('/',array('as'=>'home', 'uses'=>'IndexController@getIndex'));
Route::post('/',array('as'=>'home', 'uses'=>'IndexController@getIndex'));

So dependent of what I have selected from my dropdownlist I would go to ProjectsController or OrdersController or TasksController. To store a project or a order or a task. But the following route doesn't work:

Route::post('works',array('as'=>'user.store.work'), function() {
    $action = 'store';
    if (Input::get('worktypes')=="project") {
        return App::make('ProjectsController')->$action();  
    }
    elseif(Input::get('worktypes')=="order") {
        return App::make('OrdersController')->$action();
    }
    else{
        return App::make('TasksController')->$action();
    }
});

And that's because I get the following error:

call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

The reason why I get that error is, because I have to give 1 more argument for that expected parameter. So I should change this:

Route::post('works',array('as'=>'user.store.work'), function() {

With this:

Route::post('works',array('as'=>'user.store.work',  'uses'=>'OrdersController@store'), function() {

But I don't wanna use the store() method from the OrdersController, because then I can only store orders to the database. Gladly I'm waiting for your answer. Anyway thanks for your response.

  • 写回答

1条回答 默认 最新

  • douanrang4728 2014-10-11 21:14
    关注

    You should change your route to:

    Route::post('works',array('as'=>'user.store.work', function() {
        // your function code here
        }
    ));
    

    callback should be inside array and not as 3rd parameter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路