Home  >  Article  >  Backend Development  >  About binding operations of Laravel framework routes and controllers

About binding operations of Laravel framework routes and controllers

不言
不言Original
2018-06-12 16:48:302782browse

This article mainly introduces the binding operation method of Laravel framework routing and controller. It analyzes the operation steps, implementation methods and related precautions of binding Laravel framework routing and controller in the form of examples. Friends in need can refer to it. Next

The example of this article describes the binding operation method of Laravel framework routing and controller. Share it with everyone for your reference, the details are as follows:

The relationship between routing and controller

The routing file address is in \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 bound to anonymous functions. Although they can return views or strings, the essence is the same.

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

These two are routes bound to the controller. There are two functions under the controller class BlogController, index and showPost can be called.

So the question is, which one should I choose?

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

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

Add a new controller

The controller folder address is in the Laravel folder Next\app\Http\Controllers, we continue to use the artisan console to create a new controller

php artisan make:controller MyController

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

We modify the MyController class and create a view at the same time.

If written like this, it means that once the user accesses URL:laravel/public/mvc, the routing will be handed over to MyController for control The hello function of the controller, the hello function returns the myview view, that is, returns myview.blade.php

Let’s take a look at the code of myview.blade.php

@extends('layouts.app')
@section('content')

{{ $d1 }}

this is my view!

@endsection

Here{{ $d1 }} hopes to use the value of a variable instead, so we modify the MyController controller to

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

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Analysis of Laravel framework’s paging implementation

Analysis of the life cycle and principles of Laravel framework

Laravel framework routing settings

The above is the detailed content of About binding operations of Laravel framework routes and controllers. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn