Laravel:在另一个控制器中加载方法而不更改 url [英] Laravel: Load method in another controller without changing the url

查看:21
本文介绍了Laravel:在另一个控制器中加载方法而不更改 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个路由:Route::controller('/', 'PearsController'); 在 Laravel 中是否有可能让 PearsController 从另一个控制器加载一个方法,这样 URL 就不会'改变?

I have this route: Route::controller('/', 'PearsController'); Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?

例如:

// route:
Route::controller('/', 'PearsController');


// controllers
class PearsController extends BaseController {

    public function getAbc() {
        // How do I load ApplesController@getSomething so I can split up
        // my methods without changing the url? (retains domain.com/abc)
    }

}

class ApplesController extends BaseController {

    public function getSomething() {
        echo 'It works!'
    }

}

推荐答案

您可以使用(仅限 L3)

You can use (L3 only)

Controller::call('ApplesController@getSomething');

L4中你可以使用

$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();

在这种情况下,你必须为ApplesController定义一个路由,就像这样

In this case, you have to define a route for ApplesController, something like this

Route::get('/apples', 'ApplesController@getSomething'); // in routes.php

array() 中,如果需要,您可以传递参数.

In the array() you can pass arguments if required.

这篇关于Laravel:在另一个控制器中加载方法而不更改 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆