dporb84480 2016-05-16 08:21
浏览 55
已采纳

如何在Laravel中使用带有分形变压器的APIGuard?

I'm trying to create a web API in Laravel. I'm using the following packages for me to manage the REST return: fractal, spatie/fractal and also ApiGuard.

My controller has the following code:

<?php

namespace App\Http\Controllers;

use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController;
use App\CSV;
use App\CSVTransformer;

class ApiController extends ApiGuardController
{
    public function info()
    {
        $csvs = CSV::all();
        $estado = [0,0,0,0];

        foreach ($csvs as $csv) {
            switch ($csv->estado) {
                case "Renovado":
                    $estado[0]++;
                    break;
                case "Expirado":
                    $estado[1]++;
                    break;
                case "Aguardar Pagamento":
                    $estado[2]++;
                    break;
                case "Não Renovado":
                    $estado[3]++;
                    break;
            }
        }            

        return $this->response->withCollection($csvs, new CSVTransformer);
    }

    public function renovacoes() {
        $csvs = CSV::all();

        return json_encode([ "data" => $csvs ]);
    }
}

This is what the transformer looks like:

namespace App;

use App\CSV;
use League\Fractal\TransformerAbstract;

class CSVTransformer extends TransformerAbstract
{
    public function transform(CSV $csv)
    {
        return [
            'id'      => (int) $csv->id,
            'renovacao'   => $csv->renovacao
        ];
    }
}

The problem is, when accessing the chosen POST route to get the JSON return, the following error is thrown:

Class 'League\Fractal\TransformerAbstract' not found.

How do I solve this, so that my transformer works as it is supposed to?

EDIT:

Also, here's the CSV class:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class CSV extends Model
{
    protected $table = "csv";
}

Routes file:

Route::group(["middleware" => ["apiguard"]], function () {
    Route::group(['prefix' => 'api'], function () {
        Route::group(['prefix' => 'v1'], function () {
            Route::post("/renovations/info","ApiController@info");

            Route::post("/renovations","ApiController@renovacoes");
        });
    });
});

展开全部

  • 写回答

1条回答 默认 最新

  • douzao1119 2016-05-17 01:15
    关注

    Your vendor/league folder should look like this:

    fractal
        ├── LICENSE
        ├── composer.json
        └── src
            ├── Manager.php
            ├── Pagination
            ├── ParamBag.php
            ├── Resource
            ├── Scope.php
            ├── Serializer
            └── TransformerAbstract.php
    

    There is a TransformerAbstract.php with this content:

    <?php
    
    /*
     * This file is part of the League\Fractal package.
     *
     * (c) Phil Sturgeon <me@philsturgeon.uk>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */
    
    namespace League\Fractal;
    
    use League\Fractal\Resource\Collection;
    use League\Fractal\Resource\Item;
    use League\Fractal\Resource\NullResource;
    use League\Fractal\Resource\ResourceAbstract;
    
    /**
     * Transformer Abstract
     *
     * All Transformer classes should extend this to utilize the convenience methods
     * collection() and item(), and make the self::$availableIncludes property available.
     * Extend it and add a `transform()` method to transform any default or included data
     * into a basic array.
     */
    abstract class TransformerAbstract
    {
        /**
         * Resources that can be included if requested.
         *
         * @var array
         */
        protected $availableIncludes = [];
    
        /**
         * Include resources without needing it to be requested.
         *
         * @var array
         */
        protected $defaultIncludes = [];
    
        /**
         * The transformer should know about the current scope, so we can fetch relevant params.
         *
         * @var Scope
         */
        protected $currentScope;
    
        /**
         * Getter for availableIncludes.
         *
         * @return array
         */
        public function getAvailableIncludes()
        {
            return $this->availableIncludes;
        }
    
        /**
         * Getter for defaultIncludes.
         *
         * @return array
         */
        public function getDefaultIncludes()
        {
            return $this->defaultIncludes;
        }
    
        /**
         * Getter for currentScope.
         *
         * @return \League\Fractal\Scope
         */
        public function getCurrentScope()
        {
            return $this->currentScope;
        }
    
        /**
         * Figure out which includes we need.
         *
         * @internal
         *
         * @param Scope $scope
         *
         * @return array
         */
        private function figureOutWhichIncludes(Scope $scope)
        {
            $includes = $this->getDefaultIncludes();
            foreach ($this->getAvailableIncludes() as $include) {
                if ($scope->isRequested($include)) {
                    $includes[] = $include;
                }
            }
    
            return $includes;
        }
    
        /**
         * This method is fired to loop through available includes, see if any of
         * them are requested and permitted for this scope.
         *
         * @internal
         *
         * @param Scope $scope
         * @param mixed $data
         *
         * @return array
         */
        public function processIncludedResources(Scope $scope, $data)
        {
            $includedData = [];
    
            $includes = $this->figureOutWhichIncludes($scope);
    
            foreach ($includes as $include) {
                $includedData = $this->includeResourceIfAvailable(
                    $scope,
                    $data,
                    $includedData,
                    $include
                );
            }
    
            return $includedData === [] ? false : $includedData;
        }
    
        /**
         * Include a resource only if it is available on the method.
         *
         * @internal
         *
         * @param Scope  $scope
         * @param mixed  $data
         * @param array  $includedData
         * @param string $include
         *
         * @return array
         */
        private function includeResourceIfAvailable(
            Scope $scope,
            $data,
            $includedData,
            $include
        ) {
            if ($resource = $this->callIncludeMethod($scope, $include, $data)) {
                $childScope = $scope->embedChildScope($include, $resource);
    
                $includedData[$include] = $childScope->toArray();
            }
    
            return $includedData;
        }
    
        /**
         * Call Include Method.
         *
         * @internal
         *
         * @param Scope  $scope
         * @param string $includeName
         * @param mixed  $data
         *
         * @throws \Exception
         *
         * @return \League\Fractal\Resource\ResourceInterface
         */
        protected function callIncludeMethod(Scope $scope, $includeName, $data)
        {
            $scopeIdentifier = $scope->getIdentifier($includeName);
            $params = $scope->getManager()->getIncludeParams($scopeIdentifier);
    
            // Check if the method name actually exists
            $methodName = 'include'.str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('-', ' ', $includeName))));
    
            $resource = call_user_func([$this, $methodName], $data, $params);
    
            if ($resource === null) {
                return false;
            }
    
            if (! $resource instanceof ResourceAbstract) {
                throw new \Exception(sprintf(
                    'Invalid return value from %s::%s(). Expected %s, received %s.',
                    __CLASS__,
                    $methodName,
                    'League\Fractal\Resource\ResourceAbstract',
                    gettype($resource)
                ));
            }
    
            return $resource;
        }
    
        /**
         * Setter for availableIncludes.
         *
         * @param array $availableIncludes
         *
         * @return $this
         */
        public function setAvailableIncludes($availableIncludes)
        {
            $this->availableIncludes = $availableIncludes;
    
            return $this;
        }
    
        /**
         * Setter for defaultIncludes.
         *
         * @param array $defaultIncludes
         *
         * @return $this
         */
        public function setDefaultIncludes($defaultIncludes)
        {
            $this->defaultIncludes = $defaultIncludes;
    
            return $this;
        }
    
        /**
         * Setter for currentScope.
         *
         * @param Scope $currentScope
         *
         * @return $this
         */
        public function setCurrentScope($currentScope)
        {
            $this->currentScope = $currentScope;
    
            return $this;
        }
    
        /**
         * Create a new item resource object.
         *
         * @param mixed                        $data
         * @param TransformerAbstract|callable $transformer
         * @param string                       $resourceKey
         *
         * @return Item
         */
        protected function item($data, $transformer, $resourceKey = null)
        {
            return new Item($data, $transformer, $resourceKey);
        }
    
        /**
         * Create a new collection resource object.
         *
         * @param mixed                        $data
         * @param TransformerAbstract|callable $transformer
         * @param string                       $resourceKey
         *
         * @return Collection
         */
        protected function collection($data, $transformer, $resourceKey = null)
        {
            return new Collection($data, $transformer, $resourceKey);
        }
    
        /**
         * Create a new null resource object.
         *
         * @return NullResource
         */
        protected function null()
        {
            return new NullResource();
        }
    }
    

    Maybe you should reinstall the fractal package.

    Reinstall the package:

    • Remove the old one composer remove league/fractal
    • Optional: Update composer with the latest package versions composer update
    • Install fractal composer require league/fractal

    展开全部

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

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部