Laravel5 basic tutorial | 3. Controller: task distributor

Source: Internet
Author: User
Laravel5 basic tutorial | 3. Controller: basic tutorial for task distributor Laravel 5 | 3. Controller: task distributor-serious table
Tutorial Directory
  • 1. installation and deployment-serious table
  • 2. routing: the connection between the link and the controller-table seriousness
  • 3. Controller: submitter of the task-serious table

The controller is a small matchmaker.

Sometimes we find that it takes not long for the route to directly write the logic into the route, and the routing will become larger.

Route::get('/', function(){    ...    ...    ... // (?' ')????})

What should we do at this time?

Controller!

It generally works like this:

Notification Model: Prepare Data for me.

Notification View: you have prepared a template for me.

Return the rendered template to the browser.

Finished.

For example, the Hello part of the previous lesson can be implemented using Controller.

Route::get('hello', 'HelloController@sayHello');

Suddenly there are many simple and elegant things.
The first parameter is still the address parameter, and the second parameter is a string. @ specifies the controller, and the later part specifies the method used by the controller.

Create a Controller

You can use the following command to create a controller for both windows and unix:

Cd my/laravel root directory php artisan make: controller HelloController

If the returned result is similar to... controller created successfully..., the controller is created successfully. in this case, check/app/Http/Controllers and you will find that this directory has an additional file HelloController.

Of course, you can create a Controller manually, but generally you can use commands to meet your needs.

The content of HelloController is as follows:

...class HelloController extends Controller{    public function index()    {        }        ...}

All methods can be deleted. these methods are created by artisan for us (but I never seem to appreciate them ):

...class HelloController extends Controller{}

Create a method sayHello:

...class HelloController extends Controller{    public function sayHello($name)    {        return 'Hello' . $name . '.';    }}

The routing rule should be:

Route::get('hello/{name}', 'HelloController@sayHello');

Parameter passing in the address bar: localhost: 8888/hello/LiMing

Some people ask, if you want to make the name an option, that is, if you have a name, output "Hello + name. ", but if there is no name, output" Hello there. "Do you want to add?
This can be done in Laravel:

// Route file Route: get ('Hello'/{name ?} ', 'Hellocontroller @ sayhel'); // controller... public function sayHello ($ name = null) {if (! $ Name) return 'Hello there. '; else return 'hello'. $ name ;}...

Shameless

  • Laravel 5 video tutorial address-table lecture hall (trial)
  • All courses: biaoyansu.com

Shameless

If you have any questions, please comment at the bottom ~

Tutorial Directory
  • 1. installation and deployment-serious table
  • 2. routing: the connection between the link and the controller-table seriousness
  • 3. Controller: submitter of the task-serious table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.