skip to Main Content

I created CustomerController in Http, later, I fixed-route get customers, but getting an error in a single action Controller.

I tried to show off CustomerController view for displaying customers logged in page

coding error image

Here is my error message:

Use of undefined constant view – assumed ‘view’ (this will throw an Error in a future version of PHP)

2

Answers


  1. The first argument of the view method in the controller should be the name of the view.

    Routes/web.php

    Route::get('/', 'CustomerController');
    

    app/Http/Controllers/CustomerController.php

    <?php
    
    
    namespace AppHttpControllers;
    
    
    use IlluminateHttpRequest;
    
    class CustomerController extends Controller
    {
        public function __invoke(Request $request)
        {
            return view('customers');
        }
    }
    

    resources/views/customers.blade.php

    <h1>Customers</h1>
    
    Login or Signup to reply.
  2. Looks like you’re trying to access the old ways to render blade file look at this :-

     return View::make('customers.index', $customersList);
    

    To use view() method

    return view('admin.pages.customers.index',compact('someVaiable'));
    
    OR
    
    // You can define constant for your controller get methods
    
    private $layout;
    public function __construct()
    {
        $this->layout = 'admin.pages.customers.';
    }
    
    public function index(){
        return view($this->layout.'index');
    }
    

    Take a look a this for Single Action Controllers example

    https://laravel.com/docs/5.8/controllers#single-action-controllers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search