3

I'm working on Laravel framework, I have created a class under controllers/admin/PlacesController.php

I'm placing it in a name space controller/admin;

But as you can see below I can't use standart Laravel classes without "\" Please see \View

class PlacesController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $places=\Place::all();

        return \View::make('admin.places.index',compact('places'));
    }


    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return \View::make('admin.places.createOrEdit');
    }
}

But I would want to use it as View without "\" how can I do this? It is really a problem to fix all View to \View

Thanks all.

fobus
  • 1,938
  • 8
  • 29
  • 48

1 Answers1

5

You should import View class because it's in other namespace (root namespace).

Add:

use View;

at the beginning of your file, for example:

<?php

namespace yournamespacehere;

use View;

Now you will be able to use in your controllers return View instead of return \View

If you want more explanation you could look at How to use objects from other namespaces and how to import namespaces in PHP

Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291