Laravel 简单入门用法介绍

Larval 使用问题

  1. 数据配置

在 app/config/database.php 文件修改数据库配置后,还不能生效,必须要修改 .env 文件才能生效;

php artisan 命令执行报错: Illuminate\Foundation\ComposerScripts::postInstall

$ php artisan optimize

PHP Parse error: syntax error, unexpected ‘class’ (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or ‘{‘ or ‘$’ in /data/d2/wwwroot/baking-server/artisan on line 31
Script php artisan optimize handling the post-install-cmd event returned with error code 255

解决:直接执行 php artisan optimize 即可解决

  1. notfoundhttpexception in routecollection.php line 161 laravel 5
    Larval return error code 500

Resolve way: sudo chmod -R 777 ./storage

ErrorException in Filesystem.php line 111: file_put_contents(/data/d2/wwwroot/baking-server/bootstrap/cache/services.php): failed to open stream: Permission denied
Issue: Always not found httpexception in route collection.php line 161 larval 5

将 /project_name/app/Http/Controllers/user 目录,修改为 /project_name/app/Http/Controllers/User。
打开 URL: http://example.com/lamp/lam/user/login ,就会出现提示 UserController.php 中没有 login method。
解决方案:执行 php composer.phar dump-autoload -o

http://localhost/baking/bak/

/data/d2/wwwroot/baking-server

nginx.conf

# baking project
location  /baking/bak {
        index wc.php;
        if (!-e $request_filename){
             rewrite  ^/baking/(.*)$  /baking-server/index.php?s=$1  last;
         }
         ###access_log  /data/d2/logs/baking.access.log main;
 }
#route.php
Route::group(
  ['prefix'=>'bak'], function() {
    Route::get('/', function() {
      return view('welcome');    
  });
}
create table bak_socket_connect(
 id int(11) auto_increment,
 primary key(id),
 mac varchar(200),
 socket text
) engine = InnoDB default charset=utf8;

insert into bak_device values(‘1223234532’, ‘abcdef’, ‘abc’, 
                    ‘123456789’, ‘defaultdefault’, ’12’, ’09’, 
                    ‘shenzhen’, ’90’, ‘2016-08-10’,’2016-08-10’);

date at: August/10/2016#

**ReflectionException in Route.php line 280: Class App\Http\Controllers\User\AuthController does not exist**

Route::*get*('auth/index',  'User\AuthController@getIndex');
path-to-project/app/Http/Controllers/user/AuthController.php

<?php
    namespace App\Http\Controllers\User;
    use App\Http\Controllers\Controller;
    //use App\Http\Controllers\Wxpay\Jsapi;
    //use App\Http\Controllers\Wxpay\JsApiPay;
    //use Illuminate\Http\JsonResponse;
    //use Illuminate\Http\Request;
    //use Illuminate\Support\Facades\DB;
    
    class AuthController extends Controller{
        public function __construct()    {
            date_default_timezone_set("Asia/Shanghai");
            $this->middleware('guest');    
        } 
        public function getIndex()    {
            echo 'index';
        }
    }
  1. 如何对某些 URIs 关闭CsrfToken校验

打开 app/Http/Middleware/VerifyToken.php 文件,在 $except 数组中添加 URI 即可,如:$except = [‘lam/*’];

  1. Controller 中的数据操作
// 二维数组形式,数组里面是一个个对象
$user_info = DB::table('customer')->where($where)->get(); 
// 只返回一个对象
$user_info = DB::*table*('customer')->where($where)->first(); 
print_r($user_info);

Controller 中 function 如何使用跳转

return redirect('bak/device/devices');
  1. View 中如何使用 URL 呢?

blade 模板

<a href="{{ url('page') }}">Some Text</a>

without blade

<a href="<?php echo url('page') ?>">Some Text</a>
    原文作者:xiaojianxu
    原文地址: https://www.jianshu.com/p/54a528c3c17e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞