如何从 Laravel 5.2 中的另一个控制器调用控制器函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37692790/
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 13:57:28  来源:igfitidea点击:

How do I call a controller function from another controller in laravel 5.2?

phpapilaravellaravel-5

提问by dollar

I have 2 controllers in laravel 5.2

我在 Laravel 5.2 中有 2 个控制器

1) Apiauth controller

1) Apiauth 控制器

   <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

   use App\Http\Requests;
   use App\Api_auth;

   class Apiauth extends Controller
   {
       public function checkauth($reqauthkey)
       {
             $authkey=Api_auth::orderBy('id', 'desc')->first();


             if($authkey->authkey!=$reqauthkey)
            return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);  
        }

 }

2) MobileregistrationController

2) 移动注册控制器

        namespace App\Http\Controllers;


        use Illuminate\Http\Request;
        use App\Http\Controllers\Apiauth;
        use App\Http\Requests;
        use App\Mobile_registration;
        use App\Api_auth;

        use App\Http\Requests\CreateMobileRegistrationRequest;

        class MobileregistrationController extends Controller
        {

            public function index(Request $request)
            {

                App\Http\Controllers\Apiauth->checkauth($request->authkey);

                // $authkey=Api_auth::orderBy('id', 'desc')->first();


                // if($authkey->authkey!=$request->authkey)
                //     return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);    

                $mobileregistration=Mobile_registration::all();


                if($mobileregistration->isEmpty())
                    return response()->json(['response'=>'false','message'=>'No data found','code'=>404],404);
                else
                    return response()->json(['response'=>'true','data'=>$mobileregistration],200);
            }


            public function show($id,Request $request)
            {

                $authkey=Api_auth::orderBy('id', 'desc')->first();


                if($authkey->authkey!=$request->authkey)
                    return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);     

                $mobileregistration=Mobile_registration::find($id);

                if(!$mobileregistration)
                    return response()->json(['response'=>'false','message'=>'No data found','code'=>404],404);
                else
                    return response()->json(['response'=>'true','data'=>$mobileregistration],200);

            }

            public function store(CreateMobileRegistrationRequest $request)
            {
                $values =$request->only(['mobile_imei','mobile_number','application_type','version','isverified','reg_date_time','authkey']);

                $authkey=Api_auth::orderBy('id', 'desc')->first();


                if($authkey->authkey!=$request->authkey)
                    return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);   

                Mobile_registration::create($values);

                return response()->json(['response'=>'true','message'=>'Values Inserted','code'=>201],201);
            }

            public function update($id,CreateMobileRegistrationRequest $request)
            {
                $mobileregistration=Mobile_registration::find($id);

                if(!$mobileregistration)
                    return response()->json(['response'=>'false','message'=>'No matching data found for editing','code'=>404],404);

                $authkey=Api_auth::orderBy('id', 'desc')->first();


                if($authkey->authkey!=$request->authkey)
                    return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403); 

                $mobileregistration->mobile_imei=$request->get('mobile_imei');
                $mobileregistration->mobile_number=$request->get('mobile_number');
                $mobileregistration->application_type=$request->get('application_type');
                $mobileregistration->version=$request->get('version');
                $mobileregistration->isverified=$request->get('isverified');
                $mobileregistration->reg_date_time=$request->get('reg_date_time');

                $mobileregistration->save();

                return response()->json(['response'=>'true','message'=>'Mobile Registration details updated successfully','code'=>200],200);


            }

            public function destroy($id,Request $request)
            {
                $mobileregistration=Mobile_registration::find($id);

                if(!$mobileregistration)
                    return response()->json(['response'=>'false','message'=>'No matching data found for deleting','code'=>404],404);

                $authkey=Api_auth::orderBy('id', 'desc')->first();


                if($authkey->authkey!=$request->authkey)
                    return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403); 

                $mobileregistration->delete();

                return response()->json(['response'=>'true','message'=>'Provided details are deleted sucessfully','code'=>200],200);
            }
        }

Now in MobileregistrationController, for every function I want to call this function

现在在 MobileregistrationController 中,对于每个我想调用这个函数的函数

     public function checkauth($reqauthkey){} 

of Apiauth controller

Apiauth 控制器的

But when I used this code to call this function I am gettig error message

但是当我使用此代码调用此函数时,我收到了错误消息

    App\Http\Controllers\Apiauth->checkauth($request->authkey);

Error message

错误信息

    FatalErrorException in MobileregistrationController.php line 20:
    Class 'App\Http\Controllers\App\Http\Controllers\Apiauth' not found

I have searched lot on Stackoverflow for the answers but none of solution worked for me correctly. Someone help me to rectify this.

我在 Stackoverflow 上搜索了很多答案,但没有一个解决方案适合我。有人帮我纠正这个。

回答by mydo47

I have route

我有路线

Route::get('/test/index', 'TestController@index');

Apiauth with function test()

带有函数 test() 的 Apiauth

<?php
namespace App\Http\Controllers;

class Apiauth extends Controller {

  public function test() {
   return "abc";
  }
}

and i have another controller TestController

我有另一个控制器 TestController

<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Apiauth;

class TestController extends Controller {
  public function index() {
    $controller = new Apiauth;
    return $controller->test();
  }
}

call url /test/indexit print abc. You should try my anwser and solove your problem

调用 url/test/index打印abc。你应该试试我的回答并解决你的问题