


Analysis of Laravel framework template loading, variable allocation and simple routing functions
This article mainly introduces the Laravel framework template loading, variable allocation and simple routing functions. It analyzes the related principles, operating skills and precautions of the Laravel framework template loading, variable allocation and simple routing functions in the form of examples. Friends who need it can Refer to the examples below
This article describes the Laravel framework template loading, variable allocation and simple routing functions. Share it with everyone for your reference, the details are as follows:
As the world's number one PHP framework, it is imperative to learn Laraver. Although ThinkPHP is popular in China, it is always good for you to know one more framework.
Through the previous article's method of quickly installing the Laravel framework on a local virtual machine, we have been able to successfully install Laravel
After installation, there is a routes.php in the directory laravel\app\Http File, the point is, this is the routing file that controls the entire site.
Route::get('/', function () { return view('welcome'); });
The above is a simple route. If you bind the route and enable the pseudo-static of apche and nginx, you can use the domain name http :xxx.com/ to visit
Then a beautiful Laraver interface will appear.
So what does return refer to? It is to return a view file. Laraver’s view file is in laravel \resources\views Below. Laraver stipulates that the view file name ends with .blade.php. Usually we need a lot of views when doing projects, so we can define many directories under views, and then
return view('index.index');
All are possible. Indicates the view file in the directory below the view. Many frameworks are like this, but the file names will be different.
The above is just a simple route that calls an anonymous function, so what? Use it in conjunction with control.
Laraver’s controller directory is under laravel\app\Http\Controllers. You can use Laraver’s own
php artisen make:controller UseController
command to create a controller, and commonly used methods have been generated in it. If we output the content under the index method of the control
If necessary, there is no definition Controller, please add the --plain parameter after it
But how to access it, please see the code
Route::get('/','UseController@index');
This example binds the current directory '/' to the control The index method under UseController
Route::get('/about','UseController@about');
Another example is this, we can access the method under the specified control at http:xxx.com/about
There are many get here, for example, you need to use post Wait, I will contact you one after another in the future.
Then there is another problem, is it very troublesome to define a route every time, so Laraver allows us to use implicit controllers
Route::controller('User','UserController');
This is to access any method under User without specifying a route, but in this case remember to follow the following format in the method
Specify the delivery method Index such as get or post, and the first method name must be capitalized. If you want to pass parameters, you should write .
in function($a). Classify variables to the blade template. Note here that unlike the thinkphp framework, we commonly use the following methods:
1:
If
$name = 'php artisen';
you can
return view('index')=>with('name',$name);
and then use {{ $name }} in the template To parse the allocated variables.
The above method is equivalent to
return view('index',['a'=>'b']);
However, when parsing in the template, you still need to use {{ $a }} to allocate variables.
2:
If
$articles = DB::table('user')->get();
uses the results of database query
I have also seen someone recommend this writing method
return view('user.dashboard.index', compact('articles'));
But this is all personal operating habits.
When using the compact
function, we can directly traverse
$data = ['a','b','c'];
while using
In the case of@foreach($data as $v)
, you can directly use {{ $v }} to traverse
3:
Of course we commonly use All are allocated arrays or objects. So generally use the following method
You can
return view('index',$data);
It should be noted that by default, PDO 'fetch' in database.php under config => PDO::FETCH_ASSOC, the default is FETCH_CLASS as the object format
So when traversing, if the default settings are not modified, the traversal will be {{ $a->v }}This, if it is an array, it is {{ $a['v'] }}
About escaping and non-escaping during loading, examples are as follows:
$a = '<span style="color:red">this Laravel</span>';
{{ $a }} Output
<span style="color:red">this Laravel</span>
{{!! $aa !!}} Output in red font
'this Laravel'
Knowledge point, if the loaded variable is a one-dimensional array, the output in the template is {{ $key name}}, for example:
$data['a'] = 'this'; $data['n'] = 'that'; return view('sites.my',$data);
Then in the template
<p>我是$data分配过来的变量{{ $a }}</p>
This way OK, you cannot use
$data['a']
The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to use Laravel framework template inheritance operations
The above is the detailed content of Analysis of Laravel framework template loading, variable allocation and simple routing functions. 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

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

Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable

ThinkPHP6 is a powerful PHP framework with convenient routing functions that can easily implement URL routing configuration; at the same time, ThinkPHP6 also supports a variety of routing modes, such as GET, POST, PUT, DELETE, etc. This article will introduce how to use ThinkPHP6 for routing configuration. 1. ThinkPHP6 routing mode GET method: The GET method is a method used to obtain data and is often used for page display. In ThinkPHP6, you can use the following

How to use model events (ModelEvents) in Laravel framework Laravel framework provides many powerful features, one of which is model events (ModelEvents). Model events are a feature used in Laravel's EloquentORM (Object Relational Mapping) that allows developers to execute custom code when a specific action occurs on the model. In this article, we will explore how to use model events in the Laravel framework and provide a

How to use routing to customize page switching animation effects in a Vue project? Introduction: In the Vue project, routing is one of the functions we often use. Switching between pages can be achieved through routing, providing a good user experience. In order to make page switching more vivid, we can achieve it by customizing animation effects. This article will introduce how to use routing to customize the page switching animation effect in the Vue project. Create a Vue project First, we need to create a Vue project. You can use VueCLI to quickly build

How to use routing to implement page jump in Vue? With the continuous development of front-end development technology, Vue.js has become one of the most popular front-end frameworks. In Vue development, page jump is an essential part. Vue provides VueRouter to manage application routing, and seamless switching between pages can be achieved through routing. This article will introduce how to use routing to implement page jumps in Vue, with code examples. First, install the vue-router plugin in the Vue project.

Implementation method and experience summary of flexible configuration of routing rules in PHP Introduction: In Web development, routing rules are a very important part, which determines the corresponding relationship between URL and specific PHP scripts. In the traditional development method, we usually configure various URL rules in the routing file, and then map the URL to the corresponding script path. However, as the complexity of the project increases and business requirements change, it will become very cumbersome and inflexible if each URL needs to be configured manually. So, how to implement in PHP

In modern web applications, implementing web page navigation and routing is a very important part. Using JavaScript functions to implement this function can make our web applications more flexible, scalable and user-friendly. This article will introduce how to use JavaScript functions to implement web page navigation and routing, and provide specific code examples. Implementing web page navigation For a web application, web page navigation is the most frequently operated part by users. When a user clicks on the page
