Laravel Early Learning points of knowledge (1)

Source: Internet
Author: User

1. Currently I use the Laravel 5.2.36 version of the Web middleware to become the global middleware (do not know from 5.2.26 above the change or how, did not drill down), that is, the previous version of the route in the default will have a Route::group web middleware group, And then there's a comment on it. The general meaning is to join the Web intermediate price group protected by CSRF? Now I'm using the 5.2.36 this version cancels the following default Web middleware group, even if the entire project is added to the Web middleware. It seems to be quite handy, so when you add a middleware group, you don't need to define a Web middleware group at a time. Example:

1Route::group ([' Middleware ' =>[' admin.login '], ' prefix ' = ' admin ', ' namespace ' = ' admin '],function(){2Route::get (' index ', ' [email protected] ');3Route::get (' info ', ' [email protected] ');4Route::get (' Quit ', ' [email protected] ');5Route::any (' Pass ', ' [email protected] ');6Route::Resource(' Category ', ' Categorycontroller ');7Route::p ost (' Cate/changeorder ', ' [email protected] ');8});

' Middleware ' =>[' admin.login '] This middleware doesn't have to add Web middleware. The previous version is written like this: ' Middleware ' =>[' web ', ' admin.login ', so it's more convenient. Do not forget to add the middleware in the routes.php peer directory under the kernel.php

2. There is CSRF verification, popular point is to prevent cross-site attacks, the general meaning is that each form will send a token,token is a large string of strings. That's how I understand csrf. [For example, you want to log in to the background of a website, and the form will be sent to the background with a random token value to verify that it is valid.] Then you're allowed to log in.) So how do you add tokens? The usual front-end HTML file used in Laravel is the blade template engine. Then say it casually. Laravel front-end files exist in, resources/views this directory and then the inside of the HTML file, instead of the blade template engine, to modify the suffix of index.blade.php and then assign a view in the route can be seen, Or a controller is assigned. It is also possible to assign views within the controller. I think blade template in the Laravel is very convenient to have to sigh frame so convenient, haha [I was also the first time to learn the framework, TP also quite fire. Then I did not learn TP directly learned Laravel], pull away. Then first CSRF verification how to do other, such as the index.blade.php front-end file has a background login form, you can add {{Csrf_field ()}} in the form, the document {!!! Csrf_field ()!!} This is also possible, and there are other direct add input I will not say, I read the document. Official documentation is the most important thing to learn, and I'm just here to help you remember,

Forget to say that the use of the form when the route as far as possible to use Route::any (",") or some get method or post can not pass the error,

3. By the way, Blade template engine bar, blade annotation method above also see {{--I am the comment oh-}}, and then the following {{{URL (' Admin/code ')}} This is a reference to a verification code class, is someone else's encapsulated verification code, come directly with [ Refuse to be lazy from the start], then this is the Blade template engine URL reference method.

And one more thing. I this version of the laravel inside of the reference JS CSS, IMG and other these files if you want to use Blade template engine asset words is

{{Asset (' Style/index.css ')}} These links will be laravel guided to the public portal file. It means that you need to use the asset reference CSS JS img and so on files to be placed in the public directory, and then 5.2.2X version can be placed directly in the directory you want to be able to. Others say that it is a question of the import file, can also be modified. Specifically did not delve into. Laravel the basic knowledge of the end of the study of it,

Again a blade template engine with common file method (head and bottom), for example your head and bottom is the same, then in blade use method:

For example, your front-end directory is under the views. You only need to save the same file in a file and put it in a sibling directory.  For example, you save this file as admin.blade.php. Then add @yield (' content ') in a different content; Look at the picture:

This is then written in the file that other files need to use:

1 @extend (' admin ') {{--you save the same file suffix. balde.php do not fill in--}}  2 @section (' content ') {{-- Previously defined file variable name--}}  3  4  5 <div> This is HTML different </div> 6 <p> </p> 7 <script></script> 8  9 @endsection

If there is a directory under the same file you saved: You have to write @extend (' ever.admin ')//Note that you cannot use/

Visible blade is still very fastidious every little piece of code also has @endif similar concluding sentence. Blade can also use @if @else {} @endif; You can also use @foreach to iterate through ternary operators and so on--specific points I go to the documentation for more detailed introduction

4. Next, connect the database, Laravel connect the database first to modify the. env file

Db_prefix=blog_ This is the table prefix, if you want to use it in. Env. We're going to find it under config/database.php.

' MySQL ' =[

Some MySQL information

' Prefix ' = '//modified to: ' Prefix ' =>env (' db_prefix ', ')

]

Use a route to assign a controller to the inside Operation Demo:

1 route::get (' admin/test ', ' [email protected] ');

Then create the controller with PHP artisan Make:controller Indexcontroller.

Add a little bit of content.

 Public function index () {    $PDO = Db::connection (),getpdo ();    DD ($PDO); // this DD () and Print_r ();      

Then enter admin/test will print some PDO this object. Wait for some information. That means the connection is successful. If the error is

If you're using an editor that's not phpstrom. Your namespace may not be automatically loaded in. That's the top.

 Use illuminate\support\facades\db (This is easy to omit, the Phpstrom editor in the DB code to indicate that the direct carriage will automatically help you load and then get to use the input like this write, etc. to add.) So the use of Phpstrom development is very convenient, but WIM10 Phpstrom input method does not follow the cursor day dog, has been in the lower right corner to stay. It looks really uncomfortable.)

If you still have an error. See if the. env file has been modified.

Table Operation:

  

 1  " Span style= "color: #0000ff;" >public  function   index () { 2   $table  = db::table (' user ')->get () ; 3  DD ( $table      );  4 }  
1   Public function index () {2    $table = db::table (' user ')->where (' user_id ', 1)->get (); // Table (' table name '), if you do not need to add a prefix after you configure the table prefix. Add condition I just want the field user_id to be 1 data 3  DD ($table);      4  }

There is also the use of eloquent ORM control database, but also very convenient:

class extends model{    protected$table = ' user ';   Set table name    protected$primaryKey = ' user_id ';   Set Primary key public      $timestampsFalse;   Disable default timestamp, prevent update operation such as Updata () error }

Then use the User:: To control the database in the controller can be. Don't forget that there is no automatic introduction to loading namespaces.

Public Function index () {
$table = User::find (1);
$table->user_name= ' Akita Chia ';
$table->update ();dd($table);

}

There's more about eloquent ORM operations to view the documentation. I'm just a document Porter, just a little more of my own black and white color

Laravel Early Learning points of knowledge (1)

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.