donglei1973 2015-09-14 08:31
浏览 31
已采纳

PHP / Laravel - 如果值不在url中,则父类应停止执行

I'm trying to create a very simple api, just to understand how to manage it.

I need to check if an "api key" is passed along side the url, my url should look like:

http://myapi.app/api/users?key=my-api-key
http://myapi.app/api/orders?key=my-api-key

So I have created a parent class to manage it in order to do not replicate all the login in every controller:

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;

class ApiController extends Controller {

    protected $isValid = true;

    public function __construct()
    {
        $request = App::make('Illuminate\Http\Request');

        if( ! $request->input('api_key') )
        {
            $this->isValid = false;
        }
    }

    /**
     * Per default lo status è 200 ovvero risposta corretta
     *
     * @var int
     */
    protected $statusCode = 200;

    /**
     * @return mixed
     */
    public function getStatusCode()
    {
        return $this->statusCode;
    }

    /**
     * @param mixed $statusCode
     * @return $this
     */
    public function setStatusCode( $statusCode )
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function respondNotFound( $message = 'Risorsa non trovata' )
    {
        return $this->setStatusCode( IlluminateResponse::HTTP_NOT_FOUND )->respondWithErrors( $message );
    }

    /**
     * @param string $message
     * @return mixed
     */
    public function respondInternalError( $message = "Errore interno del server" )
    {
        return $this->setStatusCode( IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR )->respondWithErrors( $message );
    }

    /**
     * @param       $data
     * @param array $headers
     * @return mixed
     */
    public function respond( $data, $headers = [ ] )
    {
        return \Response::json( $data, $this->getStatusCode(), $headers );
    }

    /**
     * @param $message
     * @return mixed
     */
    public function respondWithErrors( $message )
    {
        return $this->respond( [
            'error' => [
                'message'     => $message,
                'status_code' => $this->getStatusCode()
            ]
        ] );
    }
} 

And this is my TestController

<?php

namespace App\Http\Controllers\Api;

class TestController extends ApiController {

    public function __construct()
    {
        parent::__construct();

        if( ! $this->isValid )
        {
            return $this->respondWithErrors("no no");
        }
    }

    public function test()
    {
        return "All ok";
    }
}

The problem is that if I visit the url with Postman or via browser and I do not set the key the test controller will always return All ok, what am I missing?

  • 写回答

1条回答 默认 最新

  • dongzhang4301 2015-09-14 08:46
    关注

    Looks like a visibility issue with your isValid variable http://php.net/manual/en/language.oop5.visibility.php

    Change the variable to be public or a better option would be to add a public getter for the isValid variable in your parent class

    public function isValid()
    {
        return $this->isValid;
    }
    

    In any case middleware might be a better option for achieving this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分