Examples of Laravel Framework Template Loading Allocating Variables and Simple Routing Features

  • 2021-10-15 09:57:05
  • OfStack

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

As the first PHP framework in the world, it is imperative to learn Laraver. Although ThinkPHP prevails in China, it is always good for you to have one more framework.

With the quick installation of Laravel framework in local virtual machine in the previous article, we can install Laravel smoothly

After installation, under the directory laravel\ app\ Http, there is an routes. php file, which is the routing file that controls the whole station.


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

The above is a simple route. If you bind the route and turn on the pseudo-static of apche and nginx, you can access it through the domain name http: xxx. com/

Then there will be a beautiful Laraver interface.

So what does return mean? It is to return a view file. The view file of Laraver is under laravel\ resources\ views. Laraver stipulates that the view file name ends with. blade. php. Usually we need many views when doing projects, so we can define many directories under views, and then in


return view('index.index');

All are OK. Indicates the view file in the directory below the view. Many frames are like this, but the file name will be different.

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

The controller directory of Laraver is under laravel\ app\ Http\ Controllers. You can use the

php artisen make:controller UseController

Command to create the controller, and the commonly used methods have been generated inside, if we output the contents under the index method of control

If you need a controller without any definition, just add the--plain parameter after it

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 another example, we can use http: xxx. com/about to access the methods below the specified control

There are many get here, such as using post and so on, which will be contacted one after another in the future.

Then there is another question, is it very troublesome to define one route at a time, so Laraver allows us to use implicit controllers


Route::controller('User','UserController');

This means accessing any 1 method under User without specifying a route, but remember to follow the following format in the method

get or post, etc. specify the transfer mode + Index, and the first method name should be capitalized. If the parameters are passed, they should be written in function ($a) and function.

Classify variables into the blade template. Note here that unlike the thinkphp framework, we often use the following methods:

1:

Suppose


$name = 'php artisen';

You can use the


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

Then use {{$name}} in the template to resolve the assigned variables.

The above method is equivalent to


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

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

2:

If


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

Using the results of database query

I also see some people recommend this writing


return view('user.dashboard.index', compact('articles'));

However, this is a personal operation habit.

In use compact Function, we can directly traverse the


return view('index.index');
0

In use


return view('index.index');
1

You can use {{$v}} directly to traverse

3:

Of course, we usually assign arrays or objects. Therefore, 1 generally uses the following methods

You can use the


return view('index.index');
2

Note that the default is database under config. PDO 'fetch' in php = > PDO:: FETCH_ASSOC, in which the default is FETCH_CLASS in object format

So when traversing, if the default settings are not modified, the traversal duration is {{$a- > v}} This, in the case of arrays, is {{$a ['v']}}

Examples of escaping and not escaping in loading are as follows:


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

{{$a}} Output


return view('index.index');
4

{{!! $aa!!}} Outputting a red font


return view('index.index');
5

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


return view('index.index');
6

In the template,


return view('index.index');
7

This way, you can't use it


$data['a']

For more readers interested in Laravel related content, please check the topics on this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this paper is helpful to the PHP programming based on Laravel framework.


Related articles: