Laravel Routing

Author: Al-mamun Sarkar Date: 2020-06-18 07:09:51

এই লেসনে লারাভেল রাউটিং নিয়ে বিস্তারিত আলোচনা করবো। লারাভেল রাউটিং এর বিভিন্ন মেথড যেমন GET, POST, PUT, PATCH, DELETE নিয়ে কাজ করবো।

 

Source code:

web.php

// Route::get('/', function () {
//     return view('welcome');
// });

Route::view('/', 'welcome');

Route::prefix('admin')->group(function() {
	Route::get('hello', function() {
		return "Hello";
	});

	Route::get('world', function() {
		return 'World';
	});

});



// Route::get('users', 'UserController@index');
// Route::get('create_user', 'UserController@create');
// Route::post('add-user', 'UserController@store');
// Route::put('update-user', 'UserController@update');
// Route::patch('update-user-name', 'UserController@updateName');
// Route::delete('delete-user', 'UserController@delete');

Route::get('user-list', 		'UserController@index')->name('user_list');
Route::get('users/{id}/profile/{email?}', 		'UserController@show');



Route::get('users/{id}', 	'UserController@display')->where('id', '[0-9]+');
Route::get('users/create',  'UserController@create');

Route::post('users', 		'UserController@store');
Route::put('users', 		'UserController@update');
Route::patch('users', 		'UserController@updateName');
Route::delete('users', 		'UserController@delete');

Route::redirect('/test', '/test-any');

Route::any('test-any', function () {
    return 'I am from Any';
});

 

UserController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
	
	public function index()
	{
		$users = User::all();

		return $users;
	}

    public function show($id, $email = 'mamun@gmail.com')
    {
        return $id . ' ' . $email;
    }

    public function display($id)
    {
        return 'Display ' . $id;
    }

    public function create()
    {
        return view('create_user');
    }

    public function store()
    {
        return 'Added a user';
    }

    public function update()
    {
        return 'Updated';
    }

    public function updateName()
    {
        return 'Name has been updated';
    }


    public function delete()
    {
        return 'Deleted';
    }

}

 

create_user.blade.php:

<!DOCTYPE html>
<html>
<head>
	<title> Create an User </title>
</head>
<body>

<form method="post" action="/test-any">
	@csrf
	@method('PUT')
	<input type="text" name="name"> <br/><br/>
	<input type="submit" value="Submit">
</form>

</body>
</html>