Skip to content

Instantly share code, notes, and snippets.

View sword-jin's full-sized avatar

Sword sword-jin

View GitHub Profile
@sword-jin
sword-jin / getUploadFileInfo.php
Last active September 21, 2015 14:13
文件上传
if(Input::has('file')) {
$file = Input::file('file');
return [
'path' => $file->getRealPath(),
'size' => $file->getSize(),
'mime' => $file->getMimeType(),
'name' => $file->getClientOriginalName(),
'extension' => $file->geClientOriginalExtension()
];
@sword-jin
sword-jin / highLightCurrentMenu.php
Created August 26, 2015 13:59
高亮当前菜单
// view.blade.php
<div class="menu">
<ul>
{{ HTML::menu_active('/','Home') }}
{{ HTML::menu_active('page/about','About') }}
{{ HTML::menu_active('page/contacts','Contacts') }}
{{ HTML::menu_active('page/service','Service') }}
</ul>
</div>
@sword-jin
sword-jin / simple-404.php
Created August 26, 2015 18:16
php userful code snippet
header('Status: 404 Not Found');
echo '<html><body><h1>Page Not Found</h1></body></html>';
@sword-jin
sword-jin / request.php
Created August 27, 2015 04:29
Symfony Http-Component
use Symfony\Component\HttpFoundation\Request;
$request = Request::createFromGlobals();
// the URI being requested (e.g. /about) minus any query parameters
$request->getPathInfo();
// retrieve GET and POST variables respectively
$request->query->get('foo');
$request->request->get('bar', 'default value if bar does not exist');
@sword-jin
sword-jin / Token.php
Last active October 23, 2015 01:43
CSRF token simple class
class Token
{
public static function generate()
{
return $_SESSION['token'] = base64_encode(openssl_random_pseudo_bytes(32));
}
public static function check($token)
{
if (isset($_SESSION['token']) && $token === $_SESSION['token']) {
@sword-jin
sword-jin / Hanler.php
Created August 28, 2015 18:09
处理 findOrFail 没有找到的 ModelNotFoundException
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
@sword-jin
sword-jin / simple.php
Created August 30, 2015 02:19
Laravel IOC understand.
class Foo
{
private $bar;
private $baz;
public function __construct(Bar $bar, Baz $baz)
{
$this->bar = $bar;
$this->baz = $baz;
}
@sword-jin
sword-jin / sql.php
Created August 30, 2015 08:40
查看当前sql
DB::enableQueryLog();
App\User::all();
print_r(DB::getQueryLog());
@sword-jin
sword-jin / iterator.php
Created August 30, 2015 09:19
laravel 中 collection.php 的部分实现,例如..使用 IteratorAggregate, Countable 接口
<?php
class Library implements IteratorAggregate, Countable
{
protected $books;
public function __construct(array $books)
{
$this->books = $books;
}
@sword-jin
sword-jin / Ioc.php
Last active August 31, 2015 09:06
简单的 Ioc 测试
<?php
class Ioc
{
protected static $registry = [];
public static function bind($name, Callable $resolver)
{
static::$registry[$name] = $resolver;
}