Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:从数据库获取数据并将修改后的数据发送到视图后,如何使用用户定义的函数? 简明的_Php_Laravel_Laravel 5.5 - Fatal编程技术网

Php Laravel:从数据库获取数据并将修改后的数据发送到视图后,如何使用用户定义的函数? 简明的

Php Laravel:从数据库获取数据并将修改后的数据发送到视图后,如何使用用户定义的函数? 简明的,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,我有一个简单的收件箱控制器 代码:InboxController 在这里,每个邮件都有id。在这里,我使用邮件id创建指向邮件的链接: <a href="{{ route('message') }}/{{$message->id}}">{{$message->subject}}</a> 您可以使用和助手。这些助手使用 与以下内容建立链接: {{ route('message') . '/' . encrypt($id) }} 然后解密它: Message:

我有一个简单的收件箱控制器

代码:InboxController 在这里,每个邮件都有id。在这里,我使用邮件id创建指向邮件的链接:

<a href="{{ route('message') }}/{{$message->id}}">{{$message->subject}}</a>
您可以使用和助手。这些助手使用

与以下内容建立链接:

{{ route('message') . '/' . encrypt($id) }}
然后解密它:

Message::find(decrypt($id));
您可以使用和助手。这些助手使用

与以下内容建立链接:

{{ route('message') . '/' . encrypt($id) }}
然后解密它:

Message::find(decrypt($id));

Laravel有助手方法,可以在代码中的任何地方使用。这些函数使用配置中设置的唯一加密密钥来设置值

所以在你看来,你可以使用

<a href="{{ route('message') }}/{{ encrypt($message->id) }}">{{$message->subject}}</a>

Laravel有助手方法,可以在代码中的任何地方使用。这些函数使用配置中设置的唯一加密密钥来设置值

所以在你看来,你可以使用

<a href="{{ route('message') }}/{{ encrypt($message->id) }}">{{$message->subject}}</a>

首先,创建一个实用程序类,并将所有方法设置为静态

public class Utility{
   public static function generate_xor_key($length)
   {
      $result = array_fill(0, $length, 0);

      for ($i = 0, $bit = 1; $i < $length; $i++) {
      for ($j = 0; $j < 3; $j++, $bit++) {
           $result[$i] |= ($bit % 2) << $j;
      }
}

return implode('', array_map('chr', $result));
}

 public static function number_encode($id, $encodedLength = 7, $rawBits = 16, $key = null)
{
$maxRawBits = $encodedLength * 3;
if ($rawBits > $maxRawBits) {
    trigger_error('number_encode(): $rawBits must be no more than 3 times greater than $encodedLength');
    return false;
}

if ($key === null) {
    $key = $this->generate_xor_key($encodedLength);
}

$result = array_fill(0, $encodedLength, 0x30);

for ($position = 0; $position < $rawBits; $position++) {
    $bit = (($id >> $position) & 0x01) << floor($position / $encodedLength);
    $index = $position % $encodedLength;
    $result[$index] |= $bit;
}

do {
    $index = $position % $encodedLength;
    $bit = ($position % 2) << floor($position / $encodedLength);
    $result[$index] |= $bit;
} while (++$position < $maxRawBits);

return implode('', array_map('chr', $result)) ^ $key;
}

public static function number_decode($id, $encodedLength = 7, $rawBits = 16, $key = null)
    {
    if ($key === null) {
        $key = $this->generate_xor_key($encodedLength);
    }

    $bytes = array_map(
    'ord',
    str_split(
        str_pad($id, $encodedLength, '0', STR_PAD_LEFT) ^ $key,
        1
       )
   );

   $result = 0;

   for ($position = 0; $position < $rawBits; $position++) {
       $index = $position % $encodedLength;
       $bit = (($bytes[$index] >> floor($position / $encodedLength)) & 0x01) << $position;
       $result |= $bit;
   }

   return $result;
     }
   }

如果您不想要一个单独的类而不是帮助函数,那么您可以按照链接进行操作。

首先创建一个实用程序类,并将所有方法设置为静态

public class Utility{
   public static function generate_xor_key($length)
   {
      $result = array_fill(0, $length, 0);

      for ($i = 0, $bit = 1; $i < $length; $i++) {
      for ($j = 0; $j < 3; $j++, $bit++) {
           $result[$i] |= ($bit % 2) << $j;
      }
}

return implode('', array_map('chr', $result));
}

 public static function number_encode($id, $encodedLength = 7, $rawBits = 16, $key = null)
{
$maxRawBits = $encodedLength * 3;
if ($rawBits > $maxRawBits) {
    trigger_error('number_encode(): $rawBits must be no more than 3 times greater than $encodedLength');
    return false;
}

if ($key === null) {
    $key = $this->generate_xor_key($encodedLength);
}

$result = array_fill(0, $encodedLength, 0x30);

for ($position = 0; $position < $rawBits; $position++) {
    $bit = (($id >> $position) & 0x01) << floor($position / $encodedLength);
    $index = $position % $encodedLength;
    $result[$index] |= $bit;
}

do {
    $index = $position % $encodedLength;
    $bit = ($position % 2) << floor($position / $encodedLength);
    $result[$index] |= $bit;
} while (++$position < $maxRawBits);

return implode('', array_map('chr', $result)) ^ $key;
}

public static function number_decode($id, $encodedLength = 7, $rawBits = 16, $key = null)
    {
    if ($key === null) {
        $key = $this->generate_xor_key($encodedLength);
    }

    $bytes = array_map(
    'ord',
    str_split(
        str_pad($id, $encodedLength, '0', STR_PAD_LEFT) ^ $key,
        1
       )
   );

   $result = 0;

   for ($position = 0; $position < $rawBits; $position++) {
       $index = $position % $encodedLength;
       $bit = (($bytes[$index] >> floor($position / $encodedLength)) & 0x01) << $position;
       $result |= $bit;
   }

   return $result;
     }
   }


如果你不想要一个单独的类而不是帮助函数,那么你可以使用link。

你使用的是来自Laravel的encode函数还是你有自己的encode函数?我使用自己的函数。您可以显示所有方法。使用自己的用户方法和Laravel函数@jerodevb但如何以及在何处定义这些函数?请将此添加到您的问题中。我添加了我的方法@Jerodevwhere,您将函数放在这里??您使用的是来自Laravel的encode函数,还是您有自己的encode函数?我使用自己的函数。您可以显示所有方法。使用自己的用户方法和Laravel函数@jerodevb但如何以及在何处定义这些函数?请将此添加到您的问题中。我添加了我的方法@Jerodevwhere,您将函数放在这里??我有自己的函数。如何使用我的功能?请在您的问题中添加此功能的防御。你如何创建这个函数?我有自己的函数。如何使用我的功能?请在您的问题中添加此功能的防御。如何创建此函数?好的,等等,我现在将在我的项目中测试您的答案是的,或者您可以将函数实现放在一个帮助文件中,并使其自动加载。我确实在带有控制器的文件夹上创建了MethodsController,并调用view和Laravel,但未找到错误类“方法”。我必须在哪里创建这个类@Sohel0415run composer dump Autolodok wait我现在将在我的项目中测试您的答案是的,或者您可以将函数实现放在帮助文件中,并使其成为Autolodedi在带有控制器的文件夹上创建了MethodsController,并调用view和Laravel,但未找到错误类“方法”。我必须在哪里创建这个类@SOHEL0415运行编写器转储自动加载
<a href="{{ route('message') }}/{{Utility::number_encode($message->id)}}">{{$message->subject}}</a>
$messages = Messages::find(Utility::number_decode($encoded_id));//do your task here after decrypt