duanjiu3486 2015-12-10 10:18
浏览 194

如何在laravel 5中实现Abstract类

in App/Abstracts/ folder I have following file

1.Transformer.php

<?php

namespace App\Abstracts;

use eventsTransformer;


abstract class Transformer
{


public function transformCollection(array $item)
{
    return array_map([$this ,'transform'],$item->toArray());
} 

public abstract function transform($event);

}

2.eventsTransformer

<?php

namespace App\Abstracts;


 class EventTransformer extends Transformer
{


public function transform($event)
    {


                return [
                        'event'=> $event['event'],
                        'date' => $event['date'],
                        'e_code'=> $event['eventcode'],
                        'country'=> $event['country'],
                        'city'=> $event['city']
                ];

    }

}

Now In my Controller I am using them like this

<?php

namespace App\Http\Controllers;
use Illuminate\Routing\ResponseFactory;
use App\Abstracts\eventsTransformer;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Event;

class EventsapiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */


     //use EventTransformer;
     protected $eventTransformer;

     public function __construct(EventTransformer $EventTransformer){

        $this->$eventTransformer = $EventTransformer; // replace 'collector' with whatever role you need.
    }


    public function index()
    {
        //
        $events =  Event::All();
        $response = array();

        return response()->json([

        'data'=>$this->eventTransformer->transform($events)

        ],200);

        //return $response['data']=$events;

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $event = Event::find($id);

        if(!$event)
        {
            return response()->json([

                'error'=> 'Event does not exist',
                'code' => 'e101'
                ],404);
        }

            return response()->json([

                'data'=>$event->toArray()

            ],200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


}

I am getting following error

ReflectionException in Container.php line 790: Class App\Http\Controllers\EventTransformer does not exist

Can any one help me out with this

MyQuestion is the same what How to include abstract in Laravel5

Thanks

  • 写回答

1条回答 默认 最新

  • drg14799 2015-12-10 19:26
    关注

    You are getting this error because you misspelled namespace.

    Instead of:

    use App\Abstracts\eventsTransformer;
    

    you should use:

    use App\Abstracts\EventTransformer;
    

    in your EventsapiController

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值