如何在laravel 5

时间:2015-12-10 10:18:44

标签: php laravel namespaces laravel-5.2

在App / Abstracts /文件夹中的

我有以下文件

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']
                ];

    }

}

现在在我的控制器中,我正在使用它们

<?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)
    {
        //
    }


}

我收到以下错误

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

任何人都可以帮我解决这个问题

MyQuestion与如何在Laravel5中包含摘要

相同

由于

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为拼写错误的命名空间。

而不是:

use App\Abstracts\eventsTransformer;

你应该使用:

use App\Abstracts\EventTransformer;

EventsapiController