当前位置:网站首页>Laravel framework knowledge

Laravel framework knowledge

2022-06-24 23:44:00 Kiway.

Laravel:

  • Implementation principle of dependency injection
    In the use of laravel When , You can see that dependency injection is heavily used . For example, in the controller HttpRequest object , Various Model Implementation classes of . The advantage of this implementation is that there is no need to new Some examples , Realize the decoupling of modules .
    Dependency injection uses PHP Reflection API Realization , Implementing dependency injection through the container can selectively load the required services , Reduce the overhead of initializing the framework .

  • Common set methods
    all() Method , Output in attribute form , Returns the underlying array represented by the collection ;
    avg() Method returns the average ;
    count() Method returns the total number of collections ;
    countBy() Method returns the number of occurrences of a value or the number of occurrences of a value specified by the callback function ;
    diff() Method returns different parts of a collection array , Combine new sets ;
    duplicates() Return duplicate values ;
    first() Return the first value after establishment ;
    flatten() Convert multidimensional array to one dimension ;
    get() Find value by key name ;
    has() Determine whether the specified key exists in the collection ;
    pop() Move out of the last value in the set ;
    slice() Returns the subsequent set of the specified value ;
    sort() Returns the subsequent set of the specified value ;
    where() Series method , Same as database conditions ;
    each() Iterates over the data items in the collection and passes each data item to a given callback .

  • Common auxiliary functions
    array_dot () Auxiliary functions allow you to convert a multidimensional array to a one-dimensional array using dot symbols .
    array_get() The function retrieves a value from a multidimensional array using a dot symbol .
    route() Function to generate a route for the specified route URL.
    dump() The function will dump Given variable , It also supports passing in multiple variables at the same time . This is very useful for debugging .
    optional() Function takes an argument , You can call methods of parameters or access properties . If the object passed is null, The method and property will return null, Instead of causing an error or throwing an exception .
    array_divide Function returns two arrays , A key that contains the original array , The other contains the values of the original array .
    array_except Function to delete the specified key from the array / It's worth it .
    array_first Function returns the first element in the array that passes the specified test .
    array_flatten Function to tile a multidimensional array into a one-dimensional array .
    array_only The function returns only the keys specified in the given array / It's worth it .
    array_plunk Function retrieves all values of a given key from an array .
    array_pull Function returns and removes the key from the array / It's worth it .
    array_random Function returns a random value from an array .
    env()
    request()
    app()
    auth()
    response()

  • Common middleware
    There are three types of middleware , They are global middleware 、 Middleware group and routing middleware :

  • Global middleware :

If you want to define the middleware in every HTTP Executed on request , Just add the corresponding middleware classes to app/Http/Kernel.php Array properties for $middleware Then you can .

  • Assign the middleware to the specified route :

If you want to assign middleware to a specific route , First of all, we should app/Http/Kernel.php File is assigned to the middleware key, By default , Of the class $routeMiddleware Property contains Laravel Built in middleware , Add your own middleware , Just append it to the back and assign it a key.

  • Middleware group

Sometimes you may want to group related middleware into the same group by specifying a key name , This makes it easier to assign them to routes , This can be done by using HTTP Kernel Provided $middlewareGroups Attribute implementation .Laravel It comes with an out of the box web and api Two middleware groups , Each contains information that can be applied to Web and API General middleware for routing .

  1. CORS Middleware can be used to add appropriate headers for responses leaving the site ( Cross domain );
  2. Log middleware can record all requests to enter the site , Thus, it is convenient for us to build the system log system .
  3. Authentication middleware
  4. CSRF Protect middleware
  • PHP Life cycle
    When we ask for a php When you file ,PHP In order to fulfill this request , It's going to happen 5 Life cycle switching of stages :
  1. Module initialization (MINIT), That is to call php.ini The initialization function of the extension specified in , Such as mysql Expand .
  2. Request initialization (RINIT), That is, to initialize the symbol table of variable name and variable value content required for executing this script , Such as $_SESSION Variable .
  3. Execute this PHP Script .
  4. Request processing complete (Request Shutdown), Call the... Of each module in order RSHUTDOWN Method , Call... For each variable unset function , Such as
    unset $_SESSION Variable .
  5. Close the module (Module Shutdown) , PHP Call... For each extension MSHUTDOWN
    Method , This is the last chance for each module to free memory . This means that there is no next request .
  • Laravel Life cycle
     Insert picture description here
原网站

版权声明
本文为[Kiway.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211117589931.html