Laravel:如何将请求重定向到控制器功能并同时使用 View::make?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15857362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:40:59  来源:igfitidea点击:

Laravel: How to redirect a request to controller function and use View::make at the same time?

phplaravellaravel-3

提问by Aristona

**Route**
Route::get('admin', function()
{
     return View::make('theme-admin.main');
});

**Controller**
class Admin_Controller extends Base_Controller {

public function action_index()
{
    echo __FUNCTION__;
}

If I forward request to controller, then I have to define View::makein every function in controller. If I don't forward it, action functiondoesn't work.

如果我将请求转发给控制器,那么我必须View::make在控制器的每个函数中定义。如果我不转发它,action function则不起作用。

Should I just forward requests to controller and use View::makeinside action functions or there are better alternatives?

我应该将请求转发给控制器并使用View::make内部操作功能还是有更好的选择?

采纳答案by Simone

Actually isn't necessary to define View::makein every function of your controllers.

实际上没有必要View::make在控制器的每个功能中定义。

You can, for example, execute an action and then redirect to another action, that could View::make.

例如,您可以执行一个动作,然后重定向到另一个动作,这可以View::make

Let's say you want to create an user and then show its profile, in a RESTful way. You could do:

假设您想创建一个用户,然后以 RESTful 方式显示其个人资料。你可以这样做:

# POST /users
public function user_create()
{
    $user = User::create(...);

    // after you have created the user, redirect to its profile
    return Redirect::to_action('users@show', array($user->id));

    // you don't render a view here!
}


# GET /users/1
public function get_show($id)
{
    return View::make('user.show');
}

回答by imal hasaranga perera

you can call the controller function like this

你可以像这样调用控制器函数

$app = app();
$controller = $app->make('App\Http\Controllers\EntryController');
return $controller->callAction('getEntry', $parameters = array());

or you can simply dispatch the request to another controller url

或者您可以简单地将请求分派到另一个控制器 url

$request = \Request::create(route("entryPiont"), 'POST', array()));
return \Route::dispatch($request);