How to return routes to views in laravel

35,632

Solution 1

In Laravel 5.5 you can now do:

Route::view('/page', 'dir.page');

Solution 2

Your should return your View.

So this will work fine:

Route::get("/page", function(){
   return View::make("dir.page");
});
Share:
35,632
Cozzbie
Author by

Cozzbie

Updated on July 24, 2020

Comments

  • Cozzbie
    Cozzbie almost 4 years

    I just tried loading a view using a route like so:

    route.php
    Route::get("/page", function(){
       return View::make("dir.page");
    });
    
    
    controller.php
    View::make("/page");
    

    ...and an error was thrown. So my question is:

    Is it possible to load a route via a view and if its possible then how?

    Thanks.

    • lozadaOmr
      lozadaOmr over 9 years
      Passing the View as a closure in the routes.php should work, you just need to add return View::make('dir.page'); Doing so, I'm not sure what you are trying to achieve with controller.php, since it won't be called by the route.
    • Cozzbie
      Cozzbie over 9 years
      Exactly my point. I didn't want to do a Redirect::to() call on the Route but a View::make() on it. Apparently that was me asking too much of Laravel. Thanks.
  • Danny
    Danny over 9 years
    @Cozzbie Sure it's causing an error. And obviously that error is View not found, cause you trying to pass Route's name to View, when you should pass a view name
  • Cozzbie
    Cozzbie over 9 years
    View name or View path and if it's View name, how do you declare them? Thanks
  • Danny
    Danny over 9 years
    @Cozzbie Yeah, sorry for this inaccuracy. I meant View path. Like if you have /views/folder/template.php you would do return View::make('folder/template');
  • Danny
    Danny over 9 years
    @Cozzbie I recommend you to check Laravel's documentation. link. It can answer almost any question you'll have at beginning.