Home > Blockchain >  Which namespace do i use for laravel view()?
Which namespace do i use for laravel view()?

Time:01-23

My ide typhints a few different namespaces for rendering a view in my controller and i'm not sure which one i'm supposed to use:

class PostsController extends Controller
{
    public function index() : View
    {
        return view('posts.index');
    }
}

The "view" function returns multiple types:

@return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
  1. So which one am i supposed to use? \Illuminate\Contracts\View\View or \Illuminate\Contracts\View\Factory
  2. What is the difference?
  3. Why is it returning two different types instead of one? I code php in a strict way because i prefer it for readable and less error prone code, in my opinion this is a bad way of doing it and as you can see is causing confusion; a method should only be allowed one return type, create multiple methods if need be.

EDIT

Thank you for your input everyone, i have come up with the following that allows me to use the facade and the contract without producing typhint errors in my ide:

<?php

namespace App\Http\Controllers;


    use Illuminate\Contracts\View\View as ViewContract;
    use Illuminate\Support\Facades\View as ViewFacade;
    
    /**
     * Class PostsController
     * @package App\Http\Controllers
     */
    class PostsController extends Controller
    {
        /**
         * @return ViewContract
         */
        public function index() : ViewContract
        {
            return ViewFacade::make('posts.index');
        }
    }

So i can call View:make() and return the contract that View:make() returns.

EDIT 2

Using the view() helper i can condense further, i am aliasing with ViewContract just for my benefit of knowing which namespace i'm using:

<?php

namespace App\Http\Controllers;


use Illuminate\Contracts\View\View as ViewContract;


/**
 * Class PostsController
 * @package App\Http\Controllers
 */
class PostsController extends Controller
{


    /**
     * @return ViewContract
     */
    public function index() : ViewContract
    {
        return view('posts.index');
    }
    
}

CodePudding user response:

I’ll try and address each of your questions.

  1. In your instance, type-hint Illuminate\Contracts\View\View or the concrete implementation (Illuminate\View\View).
  2. I’ll cover this in 3.
  3. You’re using the view global helper function. It can return different types because, well, it can. If you don’t pass a parameter to view() then it will return a view factory instance. If you do pass a parameter (like you have in your usage), then it will return an instance of the view named by the first parameter (if such a view exists). So that’s why the view() helper function is typed to return multiple different types. Because depending on how you use it, it can return a different type.

CodePudding user response:

You mean: \Illuminate\View\View.

    public function index(): \Illuminate\View\View
    {
        return view('posts.index');
    }

CodePudding user response:

view() is a helper of Illuminate\Support\Facades\View facade.

return view('posts.index');

Same as :

use Illuminate\Support\Facades\View;

return View::make('posts.index');

See the documentation of Creating & Rendering Views

  •  Tags:  
  • Related