php Laravel:在另一个控制器中加载方法而不更改 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17034616/
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
Laravel: Load method in another controller without changing the url
提问by enchance
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::controller('/', 'PearsController');
在 Laravel 中是否有可能让 PearsController 从另一个控制器加载一个方法,这样 URL 就不会改变?
For example:
例如:
// 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!'
}
}
回答by The Alpha
You can use (L3 only)
您可以使用(仅限 L3)
Controller::call('ApplesController@getSomething');
In L4
you can use
在L4
你可以使用
$request = Request::create('/apples', 'GET', array());
return Route::dispatch($request)->getContent();
In this case, you have to define a route for ApplesController
, something like this
在这种情况下,您必须为 定义一个路由ApplesController
,就像这样
Route::get('/apples', 'ApplesController@getSomething'); // in routes.php
In the array()
you can pass arguments if required.
array()
如果需要,您可以在中传递参数。
回答by Joeri
( by netoin Call a controller in Laravel 4)
Use IoC...
使用国际奥委会...
App::make($controller)->{$action}();
Eg:
例如:
App::make('HomeController')->getIndex();
and you may also give params
你也可以给参数
App::make('HomeController')->getIndex($params);
回答by Antonio Carlos Ribeiro
You should not. In MVC, controllers should not 'talk' to each other, if they have to share 'data' they should do it using a model, wich is the type of class responsible for data sharing in your app. Look:
你不应该。在 MVC 中,控制器不应该相互“交谈”,如果他们必须共享“数据”,他们应该使用模型来完成,这是负责应用程序中数据共享的类的类型。看:
// route:
Route::controller('/', 'PearsController');
// controllers
class PearsController extends BaseController {
public function getAbc()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
$something = new MySomethingModel;
$this->commonFunction();
echo $something->getSomething();
}
}
class MySomethingModel {
public function getSomething()
{
return 'It works!';
}
}
EDIT
编辑
What you can do instead is to use BaseController to create common functions to be shared by all your controllers. Take a look at commonFunction
in BaseController
and how it's used in the two controllers.
您可以做的是使用 BaseController 创建由所有控制器共享的通用功能。看看commonFunction
inBaseController
以及它是如何在两个控制器中使用的。
abstract class BaseController extends Controller {
public function commonFunction()
{
// will do common things
}
}
class PearsController extends BaseController {
public function getAbc()
{
return $this->commonFunction();
}
}
class ApplesController extends BaseController {
public function showSomething()
{
return $this->commonFunction();
}
}
回答by Kal
if you were in AbcdController
and trying to access method public function test()
which exists in OtherController
you could just do:
如果您在AbcdController
并尝试访问public function test()
存在于OtherController
您中的方法,则可以执行以下操作:
$getTests = (new OtherController)->test();
This should work in L5.1
这应该适用于 L5.1