小编Aje*_*esh的帖子

检查json对象中是否存在密钥

amt: "10.00"
email: "sam@gmail.com"
merchant_id: "sam"
mobileNo: "9874563210"
orderID: "123456"
passkey: "1234"
Run Code Online (Sandbox Code Playgroud)

以上是我正在处理的JSON对象.我想检查'merchant_id'键是否存在.我尝试了下面的代码,但它不起作用.有什么方法可以实现吗?

<script>
window.onload = function getApp()
{
  var thisSession = JSON.parse('<?php echo json_encode($_POST); ?>');
  //console.log(thisSession);
  if (!("merchant_id" in thisSession)==0)
  {
    // do nothing.
  }
  else 
  {
    alert("yeah");
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

javascript json

304
推荐指数
8
解决办法
50万
查看次数

嵌套数组验证laravel

我正在构建一个基于REST的API,其中一个API具有以下请求

{
   "categories_id" :"1",
   "product_name" : "Pen",
   "product_description" : "this is pen",
   "tags" : "pen,write",
   "image_count" : "4",
   "skus": 
      {
          "is_shippable":"n",
          "actual_price":"100.55", 
          "selling_price":"200.45",
          "quantity_type":"bucket",
          "quantity_total":"10",
          "bucket_value":"instock",
          "sort_order":"1"
      }
}
Run Code Online (Sandbox Code Playgroud)

这些是我的验证规则

protected $rules = [
        ValidatorInterface::RULE_CREATE => [
        'users_id' => 'required',
        'user_profiles_id' => 'required',
        'categories_id' => 'required',
        'product_name' => 'required|max:100',
        'product_description' => 'required|max:1000',
        'tags' => 'required',
        'image_count'=>'required|integer',
        'creation_mode'=>'required|integer',
        'skus.is_shippable'=>'in:y,n',
        'skus.actual_price'=>'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.selling_price' => 'regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
        'skus.quantity_total' => 'integer|required_if:skus.quantity_type,finite', 
        'skus.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
        'skus.sort_order'=> 'required|integer'
        ],
        ValidatorInterface::RULE_UPDATE => [
        ]
    ];
Run Code Online (Sandbox Code Playgroud)

上述请求已正确验证.但是skus可以在下面请求中包含多个实体

{
       "categories_id" …
Run Code Online (Sandbox Code Playgroud)

php validation laravel laravel-5

20
推荐指数
1
解决办法
2万
查看次数

Laravel雄辩的模型如何从关系表中获取数据

我正在开发一个具有以下雄辩模型的laravel应用程序

  • 产品hasMany('App/Sku','products_id')
  • Sku belongsTO('App/Product')

我有一个控制器'ProductController',其中提供以下代码

public function index()
{    
    $products = Product::all();
    foreach($products as $product){
            $products_id = $product->products_id;
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在公开RESTfull API,这将允许我的用户获得所有产品详细信息(包括skus,运输类型等).

假设我有一个API GET:/ products

获取所有产品详细信息的代码将是以下内容

public function index()
{              
      $products = Product::all();
      foreach($products as $product){
          $products_id = $product->products_id;
          $skus_data = Product::find($products_id)->skus;
      }
        // Now I have both the product details + skus which I can bundle into an array/json.
}
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,这个逻辑是否合适?在这种情况下,所有逻辑都在控制器中,因为我使用了雄辩的模型,我为每个表都有一个模型,并在其中定义了关系.有没有办法可以获得产品/相关模型的所有细节(产品详情(表1)+ Sku详细信息(表2))而不是使用下面的

foreach($products as $product){
    $products_id = $product->products_id;
    $skus_data = Product::find($products_id)->skus;
}
Run Code Online (Sandbox Code Playgroud)

我对laravel开发和雄辩的模型都很陌生.我将使用存储库模式进行开发,在这种情况下,aboe逻辑(Product + Sku组合)所在的位置.

请帮忙.

php repository-pattern laravel eloquent laravel-5

11
推荐指数
1
解决办法
6万
查看次数

我可以使用转换器来转换来自API而不是来自数据库的数据吗? - Laravel/Fractal

我一直在使用laravel来构建我的API.我使用变换器来转换模型对象的数据.

现在我没有数据库,而是来自API的响应作为数据源,我想将这些数据转换回用户,但我无法这样做.

我的控制器

 public function rocByName(Request $request)
    {
        try {
            $this->roc_by_name_validator->with( $request->all() )->passesOrFail();
            $company_name = $request->input('company_name');
            $result = $this->my_service->getDetailsByName($company_name); //$result has the response object from the API which I want to transform and give it as a response.

             return $this->response->collection($result,new OnboardingTransformer()); //Tried using tranformer like this
        }
        catch (ValidatorException $e) {

            dd($e);

        }

    }
Run Code Online (Sandbox Code Playgroud)

我的变形金刚

<?php

namespace Modules\Onboarding\Transformers;

use League\Fractal\TransformerAbstract;
use App\Entities\OnboardingEntity;  //I dont have an entity since the response is coming from an API!! What will give here?

/** …
Run Code Online (Sandbox Code Playgroud)

php transformer-model laravel dingo-api thephpleague-fractal

11
推荐指数
1
解决办法
2754
查看次数

Laravel + Dingo中的一致REST API响应

我一直在开发一套针对移动应用程序公开的其他API.我正在遵循Laravel项目开发的存储库模式.如何实现演示器和转换器,以便在我的所有API集合中格式化常量JSON输出?

例如,我有以下控制器用于登录

public function authenticate()
    {
        $request = Request::all();  
        try {
                // If authenticated, issue JWT token
                //Showing a dummy response
                return $token;
            }  catch (ValidatorException $e) {
                return Response::json([
                    'error'   =>true,
                    'message' =>$e->getMessageBag()
                ]);
            }
    }
Run Code Online (Sandbox Code Playgroud)

现在变压器和演示者在哪里?我知道两者都用于通过转换db对象来格式化输出,并生成格式化的JSON,以便它在我的API中保持一致.

dingo API和fractal甚至框架(L5存储库)都没有提供详细的文档,我找不到任何相关的教程.

我为另一个 API 创建了以下演示者和变换器,它提供了产品列表

namespace App\Api\V1\Transformers;

use App\Entities\Product;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract {

    public function transform(\Product $product)
    {
        return [
            'id'     => (int) $product->products_id
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

主持人

<?php

namespace App\Api\V1\Presenters;

use App\Api\V1\Transformers\ProductTransformer;
use Prettus\Repository\Presenter\FractalPresenter;

/** …
Run Code Online (Sandbox Code Playgroud)

php rest json laravel dingo-api

9
推荐指数
1
解决办法
2853
查看次数

使用Guzzle通过Curl请求传递客户端证书

我有以下curl命令

sudo curl -E openyes.crt.pem --key openyes.key.pem https://sky.myapitutorial.in:444/app/live/get
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.但是,当我试图从Guzzle做的时候,它失败了.

我无法在请求中传递客户端证书.

这是我试过的

$headers = ['Content-Type' => 'application/json','X-Client-Id' => config('mykey') , 'X-Client-Secret' => config('mykey')];

        $client = new client();

        try {
            $response = $client->post(
                $endpoint
                , 
                ['json' => $content, 'headers' => $headers,['connect_timeout' => 650]],
                [
                    'config' => [
                        'curl' => [
                            'CURLOPT_SSLKEY' => base_path().'/openyes.key.pem',
                            'CURLOPT_SSLCERT' => base_path().'/openyes.crt.pem',
                            'CURLOPT_VERBOSE' => true
                        ],
                    ]
                ],
                ['debug'=>true],
                ['http_errors' => false]
            );

            dd($response);

        }
        catch (GuzzleHttp\Exception\ClientException $e) {
            $response = $e->getResponse();
            throw $e;
        }
Run Code Online (Sandbox Code Playgroud)

我在Guzzle文档中找不到任何解决方案.

知道为什么这不起作用?

我得到的错误是 …

php ssl curl laravel guzzle

9
推荐指数
1
解决办法
4145
查看次数

为产品实施喜欢,评论和观看计数器

我创建了一个电子商务后端,我的每个产品都有以下计数器属性

- product views
- product likes
- product comments count
Run Code Online (Sandbox Code Playgroud)

我对产品数据库表的当前数据库列是

 - id
 - likes_count
 - views_count
 - comments_count
 - category_id
 - category_parent_id
 - category_sub_parent_id
 - handling_charge
 - shipping_charge
 - meetup_address
 - is_additional_fields
 - status
 - is_deleted
 - created_at
 - updated_at
Run Code Online (Sandbox Code Playgroud)

如下面的博客Wanelo工程博客所示, 实现一个经常在单行上更新的计数器会导致innodb上的行锁定,如果频繁更新会导致应用程序死锁情况.但对此的解决方案在博客中得到了很好的解释,我对此有所了解.但是,如果有多个计数器与单个产品相关联,可以在应用程序增长时同时更新.我该如何设计计数器的数据库表.我是否应该维护单独的表格

likes counter table

 - id     - product_id      - count

views counter table

 - id     - product_id     - count

comments counter table

 - id     - product_id     - count
Run Code Online (Sandbox Code Playgroud)

通过维护单独的表,即使产品同时更新(如+注释+视图),它也将单独更新,并减少行死锁情况的可能性.如果它在一个表中并且如果所有它的更新同时出现,则可能导致问题.

问题:有没有更好的方法可以为柜台设计表格?有什么建议吗?

mysql counter deadlock e-commerce

8
推荐指数
1
解决办法
816
查看次数

laravel / dingo API上的变压器用法

我正在创建一组REST API,以使用laravel存储库模式向我的移动应用程序公开。我正在使用dingo作为REST框架。我对应该如何使用转换器完成API的响应感到困惑。

我具有以下控制器功能

if(!$user) { 
    //Authenticate with Twitter and authenticate
    //Register user and issue jwt
    $user = Sentinel::register($device_details);
    $user_data = json_decode($user,true);
    $device_details['users_id'] = $user['users_id'] = $user_data['id'];
    $this->device_details->create($device_details);             
}
$token = JWTAuth::fromUser($user);
$user_array = $user->toArray();
$user_array['token'] = $token;   //An array containing user details and token
return $this->response->item($user_array, new UserTransformer)->setStatusCode(200);   //I can only pass an object (eloquent) as the #1 parameter
Run Code Online (Sandbox Code Playgroud)

我的变形金刚课

namespace App\Api\V1\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract {

    public function transform(User $user)
    {
        return [
            'users_id'     => …
Run Code Online (Sandbox Code Playgroud)

php rest json response laravel

4
推荐指数
1
解决办法
2917
查看次数

无法将加密格式从 Java 复制到 PHP

我有以下 Java 代码,由集成合作伙伴之一共享,用于其 API 加密

    import java.nio.ByteBuffer;
    import java.security.AlgorithmParameters;
    import java.security.SecureRandom;
    import java.security.spec.KeySpec;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.PBEKeySpec;
    import javax.crypto.spec.SecretKeySpec;
    import org.apache.commons.codec.binary.Base64;

public class AES256 {

    /**
     *
     * @param word
     * @param keyString
     * @return
     * @throws Exception
     */
    public static String encrypt(String word, String keyString) throws Exception {
        byte[] ivBytes;
        //String password = "zohokeyoct2017";
        /*you can give whatever you want for password. This is for testing purpose*/
        SecureRandom random …
Run Code Online (Sandbox Code Playgroud)

php java encryption aes

4
推荐指数
1
解决办法
1437
查看次数

创建只读文本字段但没有文本字段边框

我试图创建一个只读的文本字段,但它不应该看起来像文本字段.

正常的只读文本字段是

Your name is [ SAM ]
Run Code Online (Sandbox Code Playgroud)

但我想要它

Your name is SAM
Run Code Online (Sandbox Code Playgroud)

这应该看起来像句子的延续,仍然可以作为我们可以显示价值的文本字段(这里是SAM).有办法吗?

html css textfield

3
推荐指数
2
解决办法
3万
查看次数