


About binding operations of Laravel framework routes and controllers
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:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class MyController extends Controller { // }
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('myview'); } }
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') <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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Since Windows has become the gaming platform of choice, it's even more important to identify its gaming-oriented features. One of them is the ability to calibrate an Xbox One controller on Windows 11. With built-in manual calibration, you can get rid of drift, random movement, or performance issues and effectively align the X, Y, and Z axes. If the available options don't work, you can always use a third-party Xbox One controller calibration tool. Let’s find out! How do I calibrate my Xbox controller on Windows 11? Before proceeding, make sure you connect your controller to your computer and update your Xbox One controller's drivers. While you're at it, also install any available firmware updates. 1. Use Wind

Two WeChat accounts cannot be bound to the same bank card. Bind a bank card to a WeChat account: 1. Open the WeChat application, click the "Me" option, and then select the "Pay" option; 2. Select the "Add Bank Card" option and enter the bank card information as prompted; 3. Once the bank card is successfully bound, users can use the bank card to make payments and transfers in WeChat.

Tables are an essential component in many web applications. Tables usually have large amounts of data, so tables require some specific features to improve user experience. One of the important features is editability. In this article, we will explore how to implement editable tables using Vue.js and provide specific code examples. Step 1: Prepare the data First, we need to prepare the data for the table. We can use a JSON object to store the table's data and store it in the data property of the Vue instance. In this case

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

In today's era of information explosion, the construction of personal brand and corporate image has become increasingly important. As the leading fashion life sharing platform in China, Xiaohongshu has attracted a large number of user attention and participation. For those users who want to expand their influence and improve the efficiency of content dissemination, binding sub-accounts has become an effective means. So, how does Xiaohongshu bind a sub-account? How to check whether the account is normal? This article will answer these questions for you in detail. 1. How to bind a sub-account on Xiaohongshu? 1. Log in to your main account: First, you need to log in to your Xiaohongshu main account. 2. Open the settings menu: click "Me" in the upper right corner, and then select "Settings". 3. Enter account management: In the settings menu, find the "Account Management" or "Account Assistant" option and click

1. Open Toutiao. 2. Click My in the lower right corner. 3. Click [System Settings]. 4. Click [Account and Privacy Settings]. 5. Click the button on the right side of [Douyin] to bind Douyin.

How to implement API routing in the Slim framework Slim is a lightweight PHP micro-framework that provides a simple and flexible way to build web applications. One of the main features is the implementation of API routing, allowing us to map different requests to corresponding handlers. This article will introduce how to implement API routing in the Slim framework and provide some code examples. First, we need to install the Slim framework. The latest version of Slim can be installed through Composer. Open a terminal and

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create
