Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codemode's avatar

Route URL Vs View

Hi, I feel confused about the text we write in the routes file, and how we name the views. For example, what exactly is the "box" below?

Route::get('box', BoxController@index);

Is it just a url, and it does not matter if /box location actually exists or not? Also, if i want to display a list of boxes, i'll be creating a view, and probably name it "all-boxes". How does the "all-boxes" (view) url differ from "box" (route) ?

Sorry if the question is silly, i'm new. Thanks!

0 likes
4 replies
sherwinmdev's avatar

@codemode so box is the representation of the URL. Something like http://domain.com/box. You are creating the location with this code. The second argument tells the route to run the controller method. Your controller will have a return view('all-boxes'); . So when the route runs the controller method, the method calls the view so it gets loaded when the URL is accessed. Hope that makes sense. It's MVC.

codemode's avatar

Thanks @w1n78 . Does this mean i can :

Route::get('box', BoxController@index); - in the route

public function index(){ return view('all-boxes'); } - in the controller

.. and this will show in the URL http://website.com/box ... but for the content it will load all-boxes.blade.php . Did i get this right?

sherwinmdev's avatar
Level 6

@codemode yes. Now that's only loading the view. If you need to pass objects you'll need to create them in the controller method. Here's one way to do it. Apologies in advance. I'm on my phone in between breaks during jury duty. So I can't use code styling. Here goes.

public function index() {

$data['boxes'] = Box::all();

return view('all-boxes', $data);

}

Now in your view, you can put the $boxes array through a foreach loop and display the record $box->column_name

There's other ways to do it using compact(), with(), but the example above is the "common" way.

Please or to participate in this conversation.