The path is a way to create a request URL for your application. These URLs do not need to be associated with specific files on a website and are human readable and SEO friendly.
In Laravel, paths are created inside the le folder routes
. They are created in the file web.php
for websites, and within api.php
for APIs.
These route
are assigned to the group middleware
network, highlighting session status and security CSRF
. The routes in route/api.php
they are stateless and are assigned to the API middleware group.
The pre-installationdefiLaravel nita comes with two paths, one for the web and one for the API. This is what the path for web in looks like web.php
:
Route::get('/', function () {
return view('welcome');
});
All Laravel paths are definiti in the path files located within the directory routes
. The route management application, definished in the file App\Providers\RouteServiceProvider
, takes care of automatically lining up these files. The file route/web.php
definishes the paths for your web interface.
It's possible definish a path for this controller action as follows:
Route::get(‘user/{id}’, ‘UserController@show’);
Route::resource
: the method Route::resource
produces all the basic paths required for an application and is managed through the controller class.
When a request matches the specified route URI, the method is invoked show
defifinished in the controller App\Http\ControllersUserController
, passing the route parameters to the method.
For resources, you need to do two things on the application Laravel. First, you need to create a resource path on Laravel that provides insert, update, view and delete paths. Second, create a resource controller that provides a method for inserting, updating, viewing, and deleting.
The pre-installationdefiLaravel nita comes with two paths: one for the web and one for the API. Here's what the route to web looks like in web.php:
Route::get(‘/’, function () {
return view(‘welcome’);
});
Laravel Middleware acts as a bridge between the request and the reaction. It can be some kind of filter component.
Laravel work with a middleware which has the task of confirming whether the client application is verified or not. In case the client is confirmed, then routing redirects to the home page or a login page.
route
The previous code definishes a path to the home page. Whenever this route receives a request get
for /
, will return the view
welcome
.
All Laravel paths are definiti in your routing
, which are located inside the directory dei routes
. Consequently, l'AppProvidersRouteServiceProvider
of the application lines up these records. The file route/web.php
contains the routes that are managed for your web interface.
The path structure is very simple. Open the appropriate file (`web.php
` o `api.php
` ) and start the line of code with `Route:: `
, followed by the request you want to assign to that specific route and then specify the function that will be performed following the request.
Laravel offers the following path methods:
get
post
put
delete
patch
options
The paths are definited in Laravel within the Route class with HTTP, the route to reply to and the close, or the controller.
Let's see how you can create your own paths in Laravel.
Now I'm going to create a basic path that will print the times table of 2.
Route::get('/table', function () {
for($i =1; $i <= 10 ; $i++){
echo "$i * 2 = ". $i*2 ."<br>";
}
});
In the above code, I created a GET request path for the URL /table
, which will print the times table of 2 on the screen.
Now let's see the same code, parameterizing the number for which we want the multiplication table:
Route::get('/table/{number}', function ($number) {
for($i =1; $i <= 10 ; $i++){
echo "$i * $number = ". $i* $number ."<br>";
}
});
In the code the 'number
' between the braces represents the parameter, i.e. the number for which the multiplication table will be calculated. Whenever a URL of the type is specified /table/n
, then the number table will be printed n
.
There is also the way to combine both features in one path. Laravel offers the optional parameters feature which allows you to add optional parameters using the question mark '?' after the optional parameter and the pre valuedefinite. Let's see the example:
Route::get('/table/{number?}', function ($number = 2) {
for($i =1; $i <= 10 ; $i++){
echo "$i * $number = ". $i* $number ."<br>";
}
});
In the code above we created our route parameter, making the number optional, so if a user routes `/table
` then it will generate the table of 2 by defaultdefinite and if a user routes to `/table/{number}
Then the number table 'number'
will be produced.
In the previous example we created a path for generating the multiplication table, but how can we ensure that the parameter of the path is actually a number, to avoid errors when generating the multiplication table?
In Laravel, you can definish a constraint on the route parameter using the ` methodwhere
` on the route instance. The `where
` takes the parameter name and a regular expression for that parameter.
Now let's see an example of a constraint for our ` parameter{numero}
` to make sure that only a number is passed to the function.
Route:: get ( '/table/{numero?}' , funzione ( $numero = 2 ) {
for( $i = 1 ; $i < = 10 ; $i + + ) {
echo "$i * $numero = " . $i * $numero . "<br>" ;
}
} )->where( 'numero' , '[0-9]+' ) ;
In the above code, we used a regular expression for the path number. Now, if a user tries to route to /table/no will be displayed a NotFoundHttpException exception.
In Laravel, you can definish a Controller method for a path. A controller method performs all actions definite every time a user accesses the route.
With the following code we are assigning the controller method 'functionname'
to a route:
Route:: get ( '/home' , 'YourController@functionname' ) ;
The code starts with `Route::`
and then definishes the request method for the path. Subsequently, defiFinish your path and controller along with the method by adding the @ symbol before the method name.
In Laravel, you can definish a name for your path. This name is often very useful. For example, if you want to redirect a user from one location to another, you don't have to definish the full redirect URL. You can just give his name. You can definish the route name using the ` methodname
` in the route instance.
Route::get('/table/{number?}', function ($number = 2) {
for($i =1; $i <= 10 ; $i++){
echo "$i * $number = ". $i* $number ."<br>";
}
})->where('number', '[0-9]+')->name(‘table’);
Now, I could regenerate the url for this path, through the following code:
$url = route('table');
Similarly, for redirecting to this URL, the correct syntax would be:
return redirect()->route('table');
Route Groups
I Route Groups
, literally path groups, is an essential feature in Laravel, which allows you to group paths. Path groups are useful when you want to apply attributes to all grouped paths. If you use path groups, you don't have to apply the attributes individually to each path; this avoids duplication. It allows you to share attributes like middleware
o namespaces
, without defifinish these attributes on each individual path. These shared attributes can be passed in an array format as the first parameter to the method Route::group
.
Syntax of a Route Group
Route::group([], callback);
where [ ]: is an array passed to the group method as the first parameter.
Example of Route Group
in web.php
Route::group([], function()
{
Route::get('/first' , function()
{
echo "first way route" ;
});
Route::get('/second' , function()
{
echo "second way route" ;
});
Route::get('/third' , function()
{
echo "third way route" ;
});
});
In the code, defilet's find the method group(), which contains the two parameters, i.e array
e closure
. Inside the closure
, we can defifinish how many route
we want. In the above code, we have defifinished three route
.
If via browser we access the URL localhost/myproject/first
then the first one intervenes route
typing in the browser first way route
.
With the URL localhost/myproject/second
then comes the second route
typing in the browser second way route
.
While with the URL localhost/myproject/third
then comes the third route
typing in the browser third way route
.
Route Groups
The prefixes of route
they are used when we want to provide a URL structure common to multiple route
.
We can specify the prefix for all paths definites within the group using the prefix array option in Route Groups
.
Example of web.php
Route::group(['prefix' => 'movie'], function()
{
Route::get('/godfather',function()
{
echo "Godfather casting";
});
Route::get('/pulpfiction',function()
{
echo "Pulp Fiction casting";
});
Route::get('/forrestgump',function()
{
echo "Forrest Gump casting";
});
});
The code contains three paths that can be accessed from the following URLs:
/movie/godfather ---> Godfather casting
/movie/pulpfiction ---> Pulp Fiction casting
/movie/forrestgump ---> Forrest Gump casting
We can also assign middleware to all routes within a group. The middleware must be defifinished before creating the group. To see how to do this, read our article Laravel middleware how it works.
Example:
Route::middleware(['age'])->group( function()
{
Route::get('/godfather',function()
{
echo "Godfather casting";
});
Route::get('/pulpfiction',function()
{
echo "Pulp Fiction casting";
});
Route::get('/forrestgump',function()
{
echo "Forrest Gump casting";
});
});
The method name
is used to prefix each name of route
with a specified string. In the method name
, we need to specify the string with a trailing character in the prefix.
Example web.php
Route::name('movie.')->group(function()
{
Route::get('users', function()
{
return "movie.films";
})->name('films');
});
Ercole Palmeri
The naval sector is a true global economic power, which has navigated towards a 150 billion market...
Last Monday, the Financial Times announced a deal with OpenAI. FT licenses its world-class journalism…
Millions of people pay for streaming services, paying monthly subscription fees. It is common opinion that you…
Coveware by Veeam will continue to provide cyber extortion incident response services. Coveware will offer forensics and remediation capabilities…