SoFunction
Updated on 2025-03-10

Examples of Laravel framework template loading, allocation of variables and simple routing functions

This article describes the Laravel framework template loading, allocation of variables and simple routing functions. Share it for your reference, as follows:

As the world's number one PHP framework, learning Laraver is imperative. Although ThinkPHP is popular in China, knowing one more framework is always good for you.

By previous articleHow to quickly install Laravel framework in local virtual machine, We can already install Laravel successfully

After installation, under the directory laravel\app\Http, there is a file. The key point is to control the routing files of the entire site.

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

The above is a simple route. If you bind the route and enable the pseudostatic of apche and nginx, you can access it through the domain name http://

Then a beautiful Laraver interface appears.

So what does return mean? It is to return a view file, and Laraver's view file islaravel\resources\viewsBelow.Laraver specifies that the view file name ends with. Usually, we need a lot of views when doing a project, so we can define many directories under views, and then

return view('');

All are OK. It represents the view files in the directory below the view. Many frameworks are like this, but there will be differences in file names.

The above is just a simple route that calls anonymous functions, so how to use them in combination with control.

Laravel's controller directory islaravel\app\Http\ControllersBelow, you can use Laraver's own

php artisen make:controller UseController

The command creates the controller, and a commonly used method has been generated. If we output the content under the controlled index method

If you need a controller without any definition, please add the --plain parameter afterwards

But how to access it, please see the code

Route::get('/','UseController@index'); 

This example is to bind the current directory '/' to the index method under the controller UseController

Route::get('/about','UseController@about');

For example, we can access the following method specified control

There are many gets here, such as using post, etc., and you will come into contact with it one after another in the future.

Then there is another question, 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

The method of passing + Index is specified, such as get or post, and the first method name must be capitalized. If the parameter is passed, it must be written in function($a) and in function.

When categorizing variables to blade templates, note here that it is different from thinkphp frameworks. We often use the following methods:

1:

if

$name = 'php artisen';

You can

return view('index')=>with('name',$name);

Then use it in the template{{ $name }}To parse allocated variables.

The above method is equivalent to

return view('index',['a'=>'b']);

However, it is still necessary to use it when parsing it in the template{{ $a }} To allocate variables

2:

if

$articles = DB::table('user')->get();

Results obtained using database query

I have also seen someone recommend this writing method

return view('', compact('articles'));

But these are all personal operating habits.

In usecompactIn the case of functions, we can directly traverse

$data = ['a','b','c'];

In use

@foreach($data as $v)

In the case of{{ $v }}Come and go through

3:

Of course, we often use the following methods to allocate arrays or objects.

You can

return view('index',$data);

It should be noted that the default PDO 'fetch' => PDO::FETCH_ASSOC in config, the default is FETCH_CLASS as the object format

Therefore, if the default settings are not modified during traversal, the traversal time is{{ $a->v }}If this is an array, it is{{ $a['v'] }}

Regarding escape and non-escaping in loading, the following are examples:

$a = '<span style="color:red">this Laravel</span>';

{{ $a }} Output

<span style="color:red">this Laravel</span>

{{!! $aa !!}} Output red font

'this Laravel'

Knowledge point, if the loaded variable is a one-dimensional array, the output is {{ $key name }} in the template, for example:

$data['a'] = 'this';
$data['n'] = 'that';
return view('',$data);

In the template

&lt;p&gt;I am$dataAssigned variables{{ $a }}&lt;/p&gt;

This is OK, it cannot be used

$data['a']

For more information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.