How To Create Basic URLs In laravel Framework With Example

admin_img Posted By Bajarangi soft , Posted On 17-09-2020

Laravel provides several helpers to assist you in generating URLs for your application. These are mainly helpful when building links in your templates and API responses, or when generating redirect responses to another part of your application.

How To Create Basic URLs In laravel Framework With Example

Generating Basic URLs

The url helper may be used to generate arbitrary URLs for your application. The generated URL will automatically use the scheme (HTTP or HTTPS) and host from the current request:
 

$post = App\Post::find(1);

echo url("/posts/{$post->id}");

// http://example.com/posts/1
 

Accessing The Current URL

If no path is provided to the url helper, a Illuminate\Routing\UrlGenerator instance is returned, allowing you to access information about the current URL:

// Get the current URL without the query string...
echo url()->current();

// Get the current URL including the query string...
echo url()->full();

// Get the full URL for the previous request...
echo url()->previous();


Each of these methods may also be accessed via the URL facade:
use Illuminate\Support\Facades\URL;

echo URL::current();

Example(1)

1.Create route to get form.blade.php in routes\web.php
routes\web.php

 
Route::get('form', 'demoController@form');

2.Create demoController using below command
php artisan make:controller demoController

3.Open app\Http\demoController.php to implement below code 
app\Http\demoController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class demoController extends Controller
{
    public function form(){

       return view('form');
    }
   
}


4.Create form.blade.php in  resources/views after create files implement code as below shown in form.blade.php file
 
<!DOCTYPE html>
<html>
<head>
    <title>Retrieving Input In laravel</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>Retrieving Input In laravel</h1>
    </div>
    <div class="form-group col-sm-2"></div>
    <div class="form-group col-sm-8">
        <div class="well">
            <form method="post" action="">
                @csrf
                <div class="form-group col-sm-12">
                    <label>UserName</label>
                    <input  class="form-control" type="text" name="username" value="">
                </div>
                <div class="form-group col-sm-12">
                    <label>Password</label>
                    <input  class="form-control" type="text" name="password" value="">
                </div>
                <div class="form-group text-center">
                    <input  class="btn btn-primary" type="submit" >
                </div>
            </form>
            <div class="form-group col-sm-2"></div>
        </div>
    </div>
    <br>
</div>
</body>
</html>


5.Create Url to store requested data so that open demo.blade.php  and add below lines in form
demo.blade.php

<form method="post" action="{{url('filestore')}}">
 

6.Now open routes\web.php file and implement below code in it.so that when form.blade.php files calls filestore url it will goes to route and it will reach demoController

 
Route::post('formstore', 'demoController@formstore');

7.Open app\Http\demoController.php to Implement below code in it.
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class demoController extends Controller
{
   
    public function form(){

       return view('form');
    }

    public function formstore(Request $request){

        return '<h1>'.$request->path().'</h1>';
    }
  
}

Related Post