我有两个控制器SubmitPerformanceController和PrintReportController。
在PrintReportController中,我有一个叫做getPrintReport的方法。
如何在submitperformanceccontroller中访问此方法?
我有两个控制器SubmitPerformanceController和PrintReportController。
在PrintReportController中,我有一个叫做getPrintReport的方法。
如何在submitperformanceccontroller中访问此方法?
当前回答
你可以像这样访问你的控制器方法:
app('App\Http\Controllers\PrintReportController')->getPrintReport();
这可以工作,但在代码组织方面很糟糕(记住为PrintReportController使用正确的名称空间)
你可以扩展PrintReportController,这样SubmitPerformanceController将继承这个方法
class SubmitPerformanceController extends PrintReportController {
// ....
}
但这也会从PrintReportController继承所有其他方法。
最好的方法是创建一个trait(例如在app/Traits中),在那里实现逻辑,并告诉你的控制器使用它:
trait PrintReport {
public function getPrintReport() {
// .....
}
}
告诉你的控制器使用这个特性:
class PrintReportController extends Controller {
use PrintReport;
}
class SubmitPerformanceController extends Controller {
use PrintReport;
}
这两种解决方案都使SubmitPerformanceController具有getPrintReport方法,因此您可以使用$this->getPrintReport()调用它;从控制器内部或直接作为路由(如果你在routes.php中映射它)
你可以在这里阅读更多关于特质的内容。
其他回答
晚回复,但我一直在寻找这个时间。这现在可以用一种非常简单的方式实现。
如果没有参数
return redirect()->action('HomeController@index');
与参数
return redirect()->action('UserController@profile', ['id' => 1]);
文档:https://laravel.com/docs/5.6/responses # redirecting-controller-actions
在5.0中,它需要完整的路径,现在它简单多了。
这里trait完全模拟了laravel路由器的运行控制器(包括对中间件和依赖注入的支持)。仅在5.4版本中测试
<?php
namespace App\Traits;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Routing\ControllerDispatcher;
use Illuminate\Routing\MiddlewareNameResolver;
use Illuminate\Routing\SortedMiddleware;
trait RunsAnotherController
{
public function runController($controller, $method = 'index')
{
$middleware = $this->gatherControllerMiddleware($controller, $method);
$middleware = $this->sortMiddleware($middleware);
return $response = (new Pipeline(app()))
->send(request())
->through($middleware)
->then(function ($request) use ($controller, $method) {
return app('router')->prepareResponse(
$request, (new ControllerDispatcher(app()))->dispatch(
app('router')->current(), $controller, $method
)
);
});
}
protected function gatherControllerMiddleware($controller, $method)
{
return collect($this->controllerMidlleware($controller, $method))->map(function ($name) {
return (array)MiddlewareNameResolver::resolve($name, app('router')->getMiddleware(), app('router')->getMiddlewareGroups());
})->flatten();
}
protected function controllerMidlleware($controller, $method)
{
return ControllerDispatcher::getMiddleware(
$controller, $method
);
}
protected function sortMiddleware($middleware)
{
return (new SortedMiddleware(app('router')->middlewarePriority, $middleware))->all();
}
}
然后将它添加到类中并运行控制器。注意,依赖注入将与当前路由一起分配。
class CustomController extends Controller {
use RunsAnotherController;
public function someAction()
{
$controller = app()->make('App\Http\Controllers\AnotherController');
return $this->runController($controller, 'doSomething');
}
}
你可以在PrintReportController中使用一个静态方法,然后像这样从SubmitPerformanceController调用它;
namespace App\Http\Controllers;
class PrintReportController extends Controller
{
public static function getPrintReport()
{
return "Printing report";
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\PrintReportController;
class SubmitPerformanceController extends Controller
{
public function index()
{
echo PrintReportController::getPrintReport();
}
}
//In Controller A <br >
public static function function1(){
}
//In Controller B, View or anywhere <br>
A::function1();
不建议从另一个控制器调用一个控制器,但是如果出于任何原因你必须这样做,你可以这样做:
Laravel 5兼容方法
return \App::call('bla\bla\ControllerName@functionName');
注意:这不会更新页面的URL。
最好是调用Route,让它调用控制器。
return \Redirect::route('route-name-here');