我正在使用laravel 5。
我需要调用一个控制器函数,但这应该在另一个控制器中完成。
我不知道该怎么做
public function examplefunction(){
//stuff
}
Run Code Online (Sandbox Code Playgroud)
我为此功能设置了一条路线,所以在
public function otherfunctioninothercontroller(){
// I need examplefunction here
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
1)第一路
use App\Http\Controllers\OtherController;
class TestController extends Controller
{
public function index()
{
//Calling a method that is from the OtherController
$result = (new OtherController)->method();
}
}
2) Second way
app('App\Http\Controllers\OtherController')->method();
Both way you can get another controller function.
Run Code Online (Sandbox Code Playgroud)
假设我有 Controller1 和 Controller2。我想从 Controller2 中的函数内部调用 Controller1 的函数。
// Controller1.php
class Controller1 {
public static function f1()
{
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个控制器上:
// Controller2.php
use App\Http\Controllers\Controller1;
class Controller2 {
public function f2()
{
return Controller1::f1();
}
}
Run Code Online (Sandbox Code Playgroud)
注意事项:
| 归档时间: |
|
| 查看次数: |
14232 次 |
| 最近记录: |