美文网首页
Laravel Projcet Start

Laravel Projcet Start

作者: 钢铁小肥侠 | 来源:发表于2017-12-22 09:41 被阅读0次

    OS : Mac
    Runtime : Homestead

    1. New project

    A. If you create a new project

    composer create-project laravel/laravel sample --prefer-dist -vvv
    

    B. If you clone a project from git , Then install laravel

    composer install
    

    2. Set project

    A. Edit .env file

    if it is not exist , use command to generate it

    cp simple.env .env
    

    Edit it

    DB_DATABASE=db_name
    

    B. Edit hosts file

    Add 192.168.10.10   project_name.test to etc\hosts
    

    C. Edit Homestead.yaml file

    Add
      - map: project_name.test
      to: /home/vagrant/Code/project_name/public
    to sites
    Add
      - project_db_name
    to databases
    

    D. Then reload Homestead

    vagrant provision && vagrant reload
    

    3. Generate app key(Sometimes it will be init auto)

    php artisan key:generate
    

    4. Create db

    //database/migrations/[timestamp]_create_users_table.php
    php artisan make:migration create_users_table --create="users"
    php artisan migrate
    

    5. Create Model

    mkdir app/Models
    php artisan make:model Models/User
    

    6. Create controller and create route action

    php artisan make:controller UsersController
    
    public function index()
    {
      $users = User::paginate(10);
      //跳转到resources/views/users/index_blade.php,参数users
      return view('users.index', compact('users'));
    }
    

    7. Create route view

    resources/views/users/create.blade.php
    
    @foreach ($users as $user)
      {{ $user->name }}
    @endforeach
    

    8. Optimize view style

    1. Install bootstrap
    yarn install --no-bin-links
    2. Edit app.scss
    @import "node_modules/bootstrap-sass/assets/stylesheets/bootstrap";
    3. Build app.scss
    npm run dev
    npm run watch-poll
    4. Import style
    <link rel="stylesheet" href="/css/app.css">
    

    9. Set routes

    //显示所有
    //对应controller方法:index()
    Route::get('/users', 'UsersController@index')->name('users.index');
    //显示详情
    //对应controller方法:show(User $user)
    Route::get('/users/{user}', 'UsersController@show')->name('users.show');
    //创建信息的页面
    //对应Controller方法:create()
    Route::get('/users/create', 'UsersController@create')->name('users.create');
    //创建操作
    //对应Controller方法:store(Request $request)
    Route::post('/users', 'UsersController@store')->name('users.store');
    //编辑信息的页面
    //对应Controller方法:edit(User $user)
    Route::get('/users/{user}/edit', 'UsersController@edit')->name('users.edit');
    //更新操作
    //对应Controller方法:update(User $user, Request $request)
    Route::patch('/users/{user}', 'UsersController@update')->name('users.update');
    //删除操作
    //对应Controller方法:destroy(User $user)
    Route::delete('/users/{user}', 'UsersController@destroy')->name('users.destroy');
    

    Join it!

    相关文章

      网友评论

          本文标题:Laravel Projcet Start

          本文链接:https://www.haomeiwen.com/subject/yshtgxtx.html