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