This article mainly introduces the Laravel framework template loading, allocation variables and simple routing functions, combined with examples to analyze the Laravel framework template loading, allocation variables and simple routing function related principles, operating skills and considerations, the need for friends can refer to the next
This paper describes the Laravel framework template loading, assigning variables and simple routing functions. Share to everyone for your reference, as follows:
As the world's first PHP framework, learning laraver imperative, although the domestic prevalence of thinkphp, but often a framework is always good for themselves.
Through the previous article Laravel framework on the local virtual machine rapid installation method, we have been able to successfully install Laravel
After installation, in the directory laravel\app\http, there is a routes.php file, the focus, this is the control of the entire station routing files.
Route::get ('/', function () {return view (' Welcome ');});
The above is a simple route, if you bind the route, and open the pseudo-static Apche and Nginx, you can use the domain name http:xxx.com/to access
Then there will be a nice laraver interface.
So what does return mean, that is, to return a view file, laraver The view file under laravel\resources\views . LARAVER specifies that the view file name ends in. blade.php, usually we need a lot of views when we do the project, so we can define a lot of directories under views, and then
Return view (' Index.index ');
It's all right. Represents the view file under the View directory, many frameworks are like this, except that the name of the file is different.
The above is just a simple route called the anonymous function, then how to combine control to use it.
Laraver's controller directory under laravel\app\http\controllers , you can laraver your own
PHP Artisen Make:controller Usecontroller
command to create the controller, and it has already generated a common method, if we output the content under the control's Index method
If you need a controller that does not have any definitions, add the--plain parameter to it later
But how to access it, 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 ');
Again, for example, we can http:xxx.com/about to access the following method of the specified control
There are a lot of get here, such as the need to use post and so on, will come into contact later.
Then there is a problem, so it is not very troublesome to define a route each time, so laraver allows us to use the implicit controller
Route::controller (' User ', ' Usercontroller ');
This is to access any of the methods under user without specifying a route, but remember to follow the following format in the method
Get or post such as the specified delivery mode +index, method name the first to uppercase, the argument, the function ($a), in the function inside the write.
Categorical variables to the blade template, note here that unlike the thinkphp framework, we often use the following methods:
1:
If
$name = ' php Artisen ';
You can be in
Return view (' index ') =>with (' name ', $name);
Then use {{$name}} in the template to resolve the assigned variable.
The above method is equivalent to
Return view (' Index ', [' a ' = ' = ' B ']);
However, when parsing in the template, we still use {{$a}} to assign variables
2:
If
$articles = db::table (' user ')->get ();
Using database queries to derive results
I see someone recommending this.
Return view (' User.dashboard.index ', compact (' articles '));
But this is all personal operating habits.
In the compact
case of using functions, we can directly traverse
$data = [' A ', ' B ', ' C '];
When using
@foreach ($data as $v)
, you can directly use {{$v}} to traverse the
3:
Of course, we used to allocate arrays or objects. So the general use of the following methods
You can be in
Return view (' index ', $data);
It is important to note that by default, the PDO ' fetch ' in database.php is PDO::FETCH_ASSOC, and the default is Fetch_class for the object format
So at the time of traversal, if the default setting is not modified, the traversal is {{$a->v}} , if it is an array, {{$a [' V '}}
Examples of escaping and non-escaping in loading are as follows:
$a = ' <span style= ' color:red ' >this laravel</span> ';
{{$a}} output
<span style= "color:red" >this laravel</span>
{{!! $aa!}} Output of the Red font
' This Laravel '
Knowledge points, if the loaded variable is a one-dimensional array, output in the template as {{$ key name}}, for example:
$data [' a '] = ' this ' and $data [' n '] = ' that '; return view (' sites.my ', $data);
Then in the template
<p> I am the $data assigned variable {{$a}}</p>
This is OK, you can't use
$data [' A ']
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!