如何从laravel 5.2中的另一个控制器调用控制器功能?

时间:2016-06-08 03:35:39

标签: php api laravel laravel-5

我在laravel 5.2中有2个控制器

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

        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);
            }
        }

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

     public function checkauth($reqauthkey){} 
Apiauth控制器的

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

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

错误消息

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

我已经在Stackoverflow上搜索了很多答案,但没有一个解决方案对我有用。有人帮我纠正了这一点。

1 个答案:

答案 0 :(得分:1)

我有路线

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

Apiauth with function test()

<?php
namespace App\Http\Controllers;

class Apiauth extends Controller {

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

我有另一个控制器TestController

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

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

调用网址/test/index,打印abc。你应该尝试我的anwser并解决你的问题

相关问题