Laravel Framework Detailed

Source: Internet
Author: User
Tags auth define get json php file php framework require
The laravel Framework is now a very popular framework in the PHP framework

First, the basic article
1. Concept

Laravel is a young framework with a bright future, its community is vibrant, provides complete and clear documentation, and provides the necessary functionality for fast and secure development of modern applications.
Taylor Otwell first brought Laravel to the world in 2011, when Laravel was a new and modern framework. Based on the MVC architecture, Laravel can meet a variety of requirements such as event handling, user authentication, modular and extensible code through package management, and robust support for database management.
Whether you're an expert or a novice, you'll have a sense of brief encounter once you reach Laravel-this is the framework you're looking for in your PHP project. 2. Installation

1. Find the installation package, fool-type installation

2, composer installation

Download the install Package command: Composer Create-project Laravel/laravelblog

Composer General use of Composer.json in projects

eg:require Keywords: which packages are needed for your project

{

"require": {

"Monolog/monolog": "1.0.*"

   }

}

Install command: Locate the Composer.json location to execute the installation command composer Install 3. Modify Port PHP Artisan serve command laravel

(1) Eg:php artisan serve --prot = 8080 (set port)

(2) eg:php Artisan Make:model article (established model)

To see your app directory, here is not a article.php file. That's the Model file that Artisan helped us build.

(3) eg:php artisan Migrate (Database migration)

What's migration?

Open the learnlaravel5/database/migrations/2014_10_12_000000_create_users_table.php file, you can certainly see its role: using PHP to describe the structure of the database, and deploy all database structures at once using the command line

simulate inserting data

(4) Analog Insert data
PHP Artisan Make:seeder Articleseeder
PHP Artisan Db:seed

(5) eg:php artisan make:controller Admin/homecontroller Building Controller

(6) Eg:PHP artisan Make:auth Activates the Laravel auto System (Laravel uses the new features of PHP5.4 trait built-in very good simple user login registration function, suitable for some do not require complex user rights management Systems, such as simple management systems used within the company)
(7) eg:php Artisan--version View current version


See more: http://blog.csdn.net/woshihaiyong168/article/details/53197366

4. portal file, database connection, default route definition
1, laravel entry file public under the index.php

Access directory public so Laravel is running.

2. Connect to the database

. env file

db_host=127.0.0.1
Db_database=laravel5
Db_username=root
Db_password=root

See more: http://blog.csdn.net/woshihaiyong168/article/details/53197366


3, define the route (Laravel popular main reason)

Routing Location: app/https/routes.php

route::any ('/', ' homecontroller@add '); Defines the default controller and default method that can be accessed in any way

Route::p ost ('/add ', ' homecontroller@add '); Define Post Method Methods

route::get ('/delete/{id} ', ' Homecontroller@del '); Define get-mode-access values: The disadvantage must be written to the value in the method parameter

Defining routing Groups

Route::group ([' middleware ' = ' auth ', ' namespace ' = ' admin ', ' prefix ' = ' admin '], function () {
Route::get ('/', ' homecontroller@index ');
});

There are many ways to define routing: https://laravel-china.org/docs/5.1/routing#route-groups



second, since the Laravel framework is based on MVC, let 's start with the analysis

1. Frame Catalogue


2. The MVC Chapter

controller location: app/http/controllers/setting up the controller

First, the controller

(1) Controller principle Analysis:

we find the controller by routing, so where is our routing location.

app/providers/routeserviceprovider.php (routing service provider)

protected $namespace = ' app\http\controllers ';
  The routing space points to the controller location

   after the space is found, how is the namespace, class, method passed?
   vendor/laravel/framework/src/illuminate/routing/controllerdispatcher.php (Dispenser)
   
/**
 * Dispatch a request to a given controller and method.
 *
 * @param  \illuminate\routing\route  $route
 * @param  \illuminate\http\request  $request
 * @param  string  $controller
 * @param  string  $method
 * @return Mixed
 * * Public
function Dispatch (Route $route, Request $request, $controller, $method)
{
    $instance = $this->makecontroller ($controller);   
    return $this->callwithinstack ($instance, $route, $request, $method);
     Var_dump ($controller);
}
 This method is the controller that is defined in the delivery route, the method

below we take homecontroller.php as an example:
Class HomeController extends Controller
The controller locations that are inherited are:
    vendor/laravel/framework/src/illuminate/routing/controller.php

    controller.php defines an abstract class under which a register intermediary is in the controller, Perform a method in the controller, such as
 eg:
/**
 * Execute an action on the controller.
 *
 * @param  string  $method
 * @param  array   $parameters
 * @return \symfony\component\ Httpfoundation\response
 *
/Public Function callaction ($method, $parameters)
{
    return call_user _func_array ([$this, $method], $parameters);
}
 (2) Two ways to establish the controller:
      1, establish testcontroller.php in app/http/controllers/
<?php
namespace App\http\controllers;
Use app\http\requests;
Use Illuminate\http\request;
Class TestController extends Controller
{public
     function show () {      
      echo  123;   
} }
To define a route in routes.php:
Route::get ('/test ', ' testcontroller@show ');
     2, in the cmd window into the current framework directory       eg:php artisan make:controller admin/homecontroller     ("Location app/http/controllers/", Control name)
       
(3) controller operation: 
Redirect: Return redirect ()->action (' homecontroller@show ');
Return redirect (' show ')
Show the view layer and pass values:
   Return view (' Upda ', ["list"] = $list]);

Second, view layer
(1) View layer location: resources/views
The file suffix is. blade.php controller in the display view enter the name on the line
If you are under views, create a new folder controller jump (see Folder/view name)
(2) Public Templates
      public view location: resources/views/layouts/
      eg:app.blade.php    <body> @yield (' content ') </body >  

     public View Reference:
    //Inherit public template 
     @extends (' Layouts.app ')
      App is resources/views/layouts/app.blade.php can be replaced by other 

@section (' content ')
Not the same content
@endsection
A different content replaces the @yield (' content ') in the public template at this time


Display values in a view
<form action= "{{URL ('/add ')}}" method= "POST" >

<a href= "{{URL ('/del ')}}?id={{$v->id}}" > Delete </a>
<a href= "{{URL (' article/'. $article->id)}" >
eg:{{$v->id}}


@if (condition) ... @else ... @endif

Eg: @foreach ($data as $v)
<tr>
<td>{{$v->id}}</td>
<td>{{$v->title}}</td>
<td>{{$v->message}}</td>
<td>{{$v->time}}</td>
<td><a href= "delete/{{$v->id}" > Delete </a>|<a href= "upda/{{$v->id}" > Modify </a></ Td>
</tr>
@endforeach

@for ($i = 0; $i < $i + +) The current value is {$i}} @endfor
Third, the model layer use please see the blog


































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.