Routes - these are actually web addresses that you can visit in your web application. For example, / home, / profile, / dashboard and
etc. - these are different routes that can be created in a Laravel application. Note that routes are case sensitive, so / profile
is different from / Profile
B Laravel will write all of our routes in route / web.php, as
has made this directory the default for all of our web related routes. Open this file and let’s create our first route with Laravel, write to the end of this file.
The breakdown of the code above Route::get
means this is a route, which is expecting a GET request. / Sayhello
- this is the name of the route and you can create a route with any name. In addition, we have to specify what to do when this route is visited in the browser, and we do this in the form of a callback function that returns a string that says “Hey! HelloReturning webpage:
instead of just returning strings, we’re going to return web pages when someone visits the route. Let’s see how we can do this. First of all, create a file named index.blade.php
in resources / views
. In Laravel, we have a built-in Blade templating engine, so we write all our web pages in * .blade.php and
not * .html
- Program 1:
Hello! World. & Lt; / h1 >
< / html >
- Program 2:Add the following code into your web.php now.
// Create a new route
’ / viewhello’
The above code we used / viewhelloas the route name and in the callback function we used the view() method which is a built-in method provided by Laravel to serve the web page and it will automatically select the file matching from the resources / views folder. For example, passing ’ index ’ would serve as index.blade.php.Routes with controllers:Laravel gives us much more than just writing a direct callback function. We can actually make our routes point to a function inside controllers. To do this, let’s first create our controller and name it mycontroller. Just go to app / Http / Controllers and create a file named mycontroller.php. Enter the following code in this file:
- Program 1:The code below is the basic controller code where we just use the Controllers namespace to add the ability to use it , it’s like importing libraries. Let’s add the function now:
// All functions written here
// can be used in routes
- Program 2:Here we have created the index() function and inside it we use the view method to serve index2.blade.php. Now let’s create such a file in resources / views and add the following code to it:
- Program 3: We wrote the frontend file, wrote the controller, and now the last thing is - this is to register the route.
This is index 2. < / h1 >
< / html >
- Syntax:for registering routes
Route::request_type (’/ url’,’ ControllerName @ functionName’); Note.Here ControllerName - this is the name of the controller, and functionName - this is the name of the function to be used when the user visits this URL. Let’s follow this syntax and write our route in route / web.php at the end of the file:
- Program 4:Here you can see that I wrote mycontroller as my controller and index as the name of the function to be attached to this url. Now let’s go to / viewindex2 and see the result.
’mycontroller @ index’