Laravel 5 Basic Tutorials | | 3. Controller: The dispatcher of the task

Source: Internet
Author: User
Laravel 5 Basic Tutorials | | 3. Controller: Task Dispatcher-table seriousness

Tutorials Directory

  • 1. Installation and deployment-table seriousness
  • 2. Routing: Link to Controller-table seriousness
  • 3. Controller: Task Dispatcher-table seriousness

The controller is a small matchmaker.

Sometimes we find that it's not long before the logic is written directly into the route.

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

What about this time?

Controller to!

It usually does the work:

Inform model: You prepare data for me.

Notice view: You prepare the template for me.

Returns the rendered template to the browser.

Completed.

For example, the hello part of our last lesson can be implemented using a controller.

Route::get (' Hello ', ' Hellocontroller@sayhello ');

All of a sudden, a lot of neat wood.
The first argument is still the address parameter, the second parameter is the string, the specified controller before the @, and the later part specifies the method used by the controller.

Create a Controller

Both Windows and UNIX can use the following command to create a controller:

cd my/laravel root directory php artisan make:controller hellocontroller

If you return a similar ... controller created successfully ... If the controller was created successfully, check/app/http/controllers you will find that this directory has a file hellocontroller.

You can, of course, create a controller manually, but in general you can use commands to fully satisfy your needs.

At this point the content of Hellocontroller is this:

... class Hellocontroller extends controller{public    function Index ()    {        }        ...}

All methods can be removed, these methods are created for us (but I never seem to artisan):

... class Hellocontroller extends controller{}

Create a method SayHello:

... class Hellocontroller extends controller{public    function SayHello ($name)    {        return ' Hello '. $name. '.';    }}

The routing rule at this point should be:

Route::get (' Hello/{name} ', ' Hellocontroller@sayhello ');

Address Bar Reference: localhost:8888/hello/liming

A classmate asked, if you want to make the name optional, that is, if you have a name to output "Hello + name.", but if you do not have a name, direct output "Hello there." Do you want to increase it?
This can be done in Laravel:

Route file Route::get (' Hello '/{name} ', ' Hellocontroller@sayhello ');//Controller: Public Function SayHello ($name =null) {    if (! $name)        return ' Hello there. ';    else        return ' Hello '. $name;} ...

Shameless hard wide into

  • Laravel 5 Video Tutorial Address-table serious lecture (can audition)
  • All courses: biaoyansu.com

Shameless hard wide out

If you have questions, please comment below,/sprinkle flowers ~

Tutorials Directory

  • 1. Installation and deployment-table seriousness
  • 2. Routing: Link to Controller-table seriousness
  • 3. Controller: Task Dispatcher-table seriousness
  • 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.