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");
});
Author by
Cozzbie
Updated on July 24, 2020Comments
-
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 over 9 yearsPassing the View as a closure in the
routes.php
should work, you just need to addreturn 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 over 9 yearsExactly 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 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 over 9 yearsView name or View path and if it's View name, how do you declare them? Thanks
-
Danny over 9 years@Cozzbie Yeah, sorry for this inaccuracy. I meant View path. Like if you have
/views/folder/template.php
you would doreturn View::make('folder/template');
-
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.