About binding operations of Laravel framework routes and controllers

不言
Release: 2023-03-31 20:06:02
Original
2863 people have browsed it

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';
});
Copy after login

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');
Copy after login

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');
Copy after login

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
Copy after login

Then, return 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
{
  //
}
Copy after login

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

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class MyController extends Controller
{
  public function hello()
  {
    return View(&#39;myview&#39;);
  }
}
Copy after login

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(&#39;layouts.app&#39;)
@section(&#39;content&#39;)
<p class="container">
  <p class="row">
    <p class="col-md-10 col-md-offset-1">
      <p class="panel panel-default">
        <p class="panel-heading">{{ $d1 }}</p>
        <p class="panel-body">
          this is my view!
        </p>
      </p>
    </p>
  </p>
</p>
@endsection
Copy after login

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(&#39;myview&#39;,[&#39;d1&#39;=>&#39;a1&#39;]);
  }
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!