Binding operation method of routing and controller in Laravel framework

  • 2021-10-16 01:18:37
  • OfStack

In this paper, an example is given to describe the binding operation method of Laravel framework routing and controller. Share it for your reference, as follows:

Relationship between route and controller

The route file address is\ app\ Http\ routes. php. Let's look at two different routes.


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

The above are all routes that bind anonymous functions. Although they can return views and strings, they are all 1 in nature.


Route::get('/blog','BlogController@index');
Route::get('/post/{slug}','BlogController@showPost');

These two are the routes that bind the controller. There are two functions under the controller class BlogController, index and showPost, which can be called.

Then the question comes, which one should be chosen?

You can't write complex business logic in an anonymous function, so you should learn to create new controllers.


Route::get('/mvc', 'MyController@hello');

Add a new controller

The controller folder address is under the Laravel folder\ app\ Http\ Controllers, and we continue to create a new controller using the artisan console


php artisan make:controller MyController

Then, back to the controller directory, a new MyController. php file is created with the following code:


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class MyController extends Controller
{
  //
}

We modify the MyController class and create another view.


<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class MyController extends Controller
{
  public function hello()
  {
    return View('myview');
  }
}

If you write this, it means that once the user accesses URL: laravel/public/mvc, the route is handed to the hello function of the MyController controller, and the hello function returns the myview view, that is, myview. blade. php

Let's look at the code for myview. blade. php


@extends('layouts.app')
@section('content')
<div class="container">
  <div class="row">
    <div class="col-md-10 col-md-offset-1">
      <div class="panel panel-default">
        <div class="panel-heading">{{ $d1 }}</div>
        <div class="panel-body">
          this is my view!
        </div>
      </div>
    </div>
  </div>
</div>
@endsection

Here's {{ $d1 }} We want to replace it with the value of 1 variable, so we modify the MyController controller to


class MyController extends Controller
{
  public function hello()
  {
    return View('myview',['d1'=>'a1']);
  }
}

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: