Home Backend Development PHP Tutorial Functionality for Laravel framework template loading and assigning variables and simple routing

Functionality for Laravel framework template loading and assigning variables and simple routing

Jun 19, 2018 pm 03:19 PM
laravel framework routing

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.

1

2

3

Route::get('/', function () {

 return view('welcome');

});

Copy after login

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

Then a beautiful Laraver interface will appear.

So what does return refer to? It is to return a View files, Laraver's view files are under laravel\resources\views. Laraver stipulates that the view file name ends with .blade.php. Usually we need a lot of views when doing projects, so we can under views It is possible to define many directories and then place them in

1

return view('index.index');

Copy after login

. It represents the view file in the directory below the view. Many frameworks are like this, but the file name is The naming will be different.

The above is just a simple route that calls an anonymous function, so how to use it in conjunction with control.

Laraver’s controller directory is in laravel\app\Http \Controllers Below, you can create a controller through the

php artisen make:controller UseController

command that comes with Laraver, and the commonly used method, if we output the content under the index method of the control

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

But how to access it? Please look at the code

1

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

Copy after login

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

1

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

Copy after login

Another example is this, we can use http:xxx.com/about to access the specified control method

There are many get here, such as needing to use post, etc. , 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

1

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

Copy after login

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 delivery methods such as get or post Index, the first method name should be capitalized. If passing parameters, it should be in function ($a), and write .

in the function. Classify the variable to the blade template. Note here that it is different from the thinkphp framework. We commonly use it. The following methods:

1:

If

1

$name = 'php artisen';

Copy after login

you can

1

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

Copy after login

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

The above method is equivalent In

1

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

Copy after login

However, when parsing in the template, you still need to use {{ $a }} to allocate variables

2:

If

1

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

Copy after login

the result of using database query

is also I saw someone recommending this way of writing

1

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

Copy after login

But this is all personal operating habits.

When using the compact function In this case, we can directly traverse

1

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

Copy after login

in the case of using

1

@foreach($data as $v)

Copy after login

Next, you can directly use {{ $v }} to traverse

3:

Of course we usually allocate arrays or objects Come over. So generally use the following method

You can

1

return view('index',$data);

Copy after login

It should be noted that the default is PDO in database.php under config 'fetch' => 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 kind, if it is an array, it is {{ $a['v'] }}

About escaping and not escaping during loading Meaning, for example:

1

$a = &#39;<span style="color:red">this Laravel</span>&#39;;

Copy after login

##{{ $a }} Output

1

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

Copy after login

{{!! $aa !!}} Output the

1

&#39;this Laravel&#39;

Copy after login

knowledge points in red font, if loaded The variable is a one-dimensional array, which is output as {{ $key name}} in the template. For example:

1

2

3

$data[&#39;a&#39;] = &#39;this&#39;;

$data[&#39;n&#39;] = &#39;that&#39;;

return view(&#39;sites.my&#39;,$data);

Copy after login

is

## in the template.

#

1

<p>我是$data分配过来的变量{{ $a }}</p>

Copy after login

This works, you can’t use it

1

$data[&#39;a&#39;]

Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于Laravel中重写资源路由自定义URL的实现方法

Laravel5框架中向视图传送array的学习

The above is the detailed content of Functionality for Laravel framework template loading and assigning variables and simple routing. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How to implement API routing in the Slim framework How to implement API routing in the Slim framework Aug 02, 2023 pm 05:13 PM

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

Java Apache Camel: Building a flexible and efficient service-oriented architecture Java Apache Camel: Building a flexible and efficient service-oriented architecture Feb 19, 2024 pm 04:12 PM

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

How to use routing in ThinkPHP6 How to use routing in ThinkPHP6 Jun 20, 2023 pm 07:54 PM

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 routing to customize page switching animation effects in a Vue project? How to use routing to customize page switching animation effects in a Vue project? Jul 21, 2023 pm 02:37 PM

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 model events (Model Events) in Laravel framework How to use model events (Model Events) in Laravel framework Jul 28, 2023 am 10:49 AM

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

Implementation method and experience summary of flexibly configuring routing rules in PHP Implementation method and experience summary of flexibly configuring routing rules in PHP Oct 15, 2023 pm 03:43 PM

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

Use JavaScript functions to implement web page navigation and routing Use JavaScript functions to implement web page navigation and routing Nov 04, 2023 am 09:46 AM

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

How to use routing to implement page jump in Vue? How to use routing to implement page jump in Vue? Jul 21, 2023 am 08:33 AM

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.

See all articles