Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using function in name space without back-slash

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.

like image 434
fobus Avatar asked Dec 07 '25 19:12

fobus


1 Answers

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

like image 89
Marcin Nabiałek Avatar answered Dec 10 '25 09:12

Marcin Nabiałek