小编eld*_*blz的帖子

如何找出文件“mime-type(Content-Type?)”?在 Windows 上

有没有办法在 Windows 批处理/PowerShell 中找出文件的 MIME-TYPE(或者它被称为“Content-Type”...)?

windows content-type batch-file

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

Laravel否定本地范围

我在我的项目中使用laravel 5.2我刚刚实现了一个本地范围(为简单起见,使用laravel docs示例-https://laravel.com/docs/master/eloquent#local-scopes )

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Scope a query to only include popular users.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePopular($query)
    {
        return $query->where('votes', '>', 100);
    }

}
Run Code Online (Sandbox Code Playgroud)

我打电话:

$users = App\User::popular()->get();
Run Code Online (Sandbox Code Playgroud)

它就像一个魅力

可以否定范围吗?

$users = App\User::Notpopular()->get();
Run Code Online (Sandbox Code Playgroud)

还是我必须手动实现上述范围的否定?如果是这样,有更好的方法吗?

先感谢您!

php laravel eloquent laravel-5

6
推荐指数
2
解决办法
1949
查看次数

编程逻辑最佳实践 - 冗余检查

我正在创建一个大型的PHP项目,我对如何继续进行一个微不足道的疑问.

假设我们有一个类books,在这个类我有方法ReturnInfo:

function ReturnInfo($id) {
    if( is_numeric($id) ) {
        $query = "SELECT * FROM books WHERE id='" . $id . "' LIMIT 1;";

        if( $row = $this->DBDrive->ExecuteQuery($query, $FetchResults=TRUE) )   {  
                return $row;
        } else {
            return FALSE;
        }
    } else {
        throw new Exception('Books - ReturnInfo - id not valid.');          
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我有另一种方法 PrintInfo

function PrintInfo($id) {
    print_r( $this->ReturnInfo($id) );
}
Run Code Online (Sandbox Code Playgroud)

显然,代码示例仅是示例,而不是实际的生产代码.

在第二种方法中,我应该(再次)检查id是否为数字?或者我可以跳过它,因为在第一种方法中已经注意了,如果它不是异常将被抛出?

直到现在我总是用冗余支票编写代码(无论是否已在其他地方检查过,我也会在这里查看)

有最好的做法吗?只是常识吗?

提前感谢您的回复.

php logic coding-style

3
推荐指数
1
解决办法
420
查看次数

如何将用户对象绑定到中间件中的请求

我正在Laravel Spark 1.0(Laravel 5.2)中编写应用程序.我为代理(api)身份验证编写了一个自定义中间件.这是代码:

<?php

namespace App\Http\Middleware;

use App\Agent;
use Closure;
use Illuminate\Http\Request;

class AgentAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if( isset($request->token) &&  !empty($request->token) )
        {
            $agent = Agent::where('token', '=', $request->token)->first();

            if( $agent != NULL )
            {
                $team = $agent->Team()->first();
                $user = $team->User()->first();

                $request->merge(['team' => $team ]);
                $request->merge(['user' => $user ]);

                return $next($request);
            }
            else {
                return response('Unauthorized 2.', …
Run Code Online (Sandbox Code Playgroud)

php middleware request laravel laravel-spark

3
推荐指数
1
解决办法
6131
查看次数