Basic Routing
1.Create new project using below command in laravel
composer create-project --prefer-dist laravel/laravel blog
Closure
, providing a very simple and expressive method of defining routes:
Route::get('Testing', function () {
return 'Hello World,Welcome to Bajarangisoft';
});
http://localhost/blog/public/Testing
All Laravel routes are defined in your route files, which are located in the routes
directory. These files are automatically loaded by the framework. The routes/web.php
file defines routes that are for your web interface. These routes are assigned the web
middleware group, which provides features like session state and CSRF protection. The routes in routes/api.php
are stateless and are assigned the api
middleware group.
For most applications, you will begin by defining routes in your routes/web.php
file. The routes defined in routes/web.php
may be accessed by entering the defined route's URL in your browser.
For example, you may access the following route by navigating to http://your-app.test/user
in your browser:
Route::get('/user', 'UserController@index');
For Example
1.Create route in routes\web.php file as below
Route::get('/demo', 'demoController@index');
2.Create Controller using below command
php artisan make:controller demoController
3.Open App\Http\Controller\demoController.php and implement code as below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class demoController extends Controller
{
public function index(){
return 'Hello World,Welcome to Bajarangisoft';
}
}
4.Pass below Url in your google chrome and see above example output.
http://localhost/blog/public/demo
Routes defined in the routes/api.php
file are nested within a route group by the RouteServiceProvider
. Within this group, the /api
URI prefix is automatically applied so you do not need to manually apply it to every route in the file. You may modify the prefix and other route group options by modifying your RouteServiceProvider
class.
Available Router Methods
The router allows you to register routes that respond to any HTTP verb:
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Sometimes you may need to register a route that responds to multiple HTTP verbs. You may do so using the match
method. Or, you may even register a route that responds to all HTTP verbs using the any
method:
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
CSRF Protection
Any HTML forms pointing to POST
, PUT
, PATCH
, or DELETE
routes that are defined in the web
routes file should include a CSRF token field. Otherwise, the request will be rejected. You can read more about CSRF protection in the CSRF documentation:
<form method="POST" action="/profile">
@csrf
...
</form>