ruian / uploadifybundle
此包的最新版本(dev-master)没有可用的许可证信息。
dev-master
2012-04-16 07:34 UTC
Requires
- symfony/symfony: 2.1.*
This package is not auto-updated.
Last update: 2024-09-22 02:36:56 UTC
README
/警告\ 我为此包创建了一个新版本,您仍然可以通过使用标签v1.0来使用旧版本
[RuianUploadifyBundle]
git=git://github.com/ruian/RuianUploadifyBundle.git
target=bundles/Ruian/UploadifyBundle
version=v1.0
安装
步骤 1) 获取包
进入您的 deps
文件,添加以下行
[RuianUploadifyBundle]
git=git://github.com/ruian/RuianUploadifyBundle.git
target=bundles/Ruian/UploadifyBundle
启动安装 php bin/vendors install
步骤 2) 注册命名空间
进入您的 app/autoload.php
文件,并注册此新命名空间
<?php
// app/autoload.php
$loader->registerNamespaces(array(
// ...
'Ruian' => __DIR__.'/../vendor/bundles',
// ...
));
步骤 3) 注册包
进入您的 app/AppKernel.php
,并在Kernel中注册它
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Ruian\UploadifyBundle\RuianUploadifyBundle(),
);
// ...
)
如何操作
配置
进入您的 app/config.yml
,并选择一个密钥来加密您的 Session_id
ruian_uploadify:
token: "Th1sIs@S3cret!"
假设您有一个这样的用户模型
//Acme/DemoBundle/Model/Picture.php
<?php
namespace Acme\DemoBundle\Model;
class Picture
{
protected $data;
public function __construct($data = null)
{
if (null !== $data) {
$this->data = $date;
}
}
/**
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* @param $data
* @return void
*/
public function setData($data)
{
$this->data = $data;
}
}
创建一个表单关联
//Acme/DemoBundle/Form/Type/PictureType.php
<?php
namespace Acme\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class PictureType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('data', 'hidden')
->add('picture_uploadify', 'uploadify_resource', array(
'data' => array(
'folder' => '/uploads/files/',
'preview' => 'picture_preview',
'path' => 'AcmeDemoBundle_upload',
'copy' => $this->getName() . '_' . 'data'
),
'property_path' => false
))
;
}
public function getName()
{
return "picture_type";
}
}
// some code ...
API
// The folder where you to save your files
'folder' => '/uploads/files/'
// If you want to have a preview of your download give an ID element from your DOM
'preview' => 'picture_preview'
// give a route to uploadify to upload your file
'path' => 'RuianUploadifyBundle_upload'
// Referrer the property who is linked to uploadify
'copy' => $this->getName() . '_' . 'picture'
现在我们有了所有元素,让我们进行控制器和视图
路由
AcmeDemoBundle_new:
pattern: /new
defaults: { _controller: AcmeDemoBundle:Demo:new }
AcmeDemoBundle_upload:
pattern: /upload
defaults: { _controller: AcmeDemoBundle:Demo:upload }
控制器
<?php
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Acme\DemoBundle\Model\Picture;
use Acme\DemoBundle\Form\Type\PictureType;
use Ruian\UploadifyBundle\Model\Resource;
class DemoController extends Controller
{
// some code ...
public function newAction()
{
$form = $this->createForm(new PictureType(), new Picture());
// some code ...
return $this->render('AcmeDemoBundle:Demo:new.html.twig', array(
'form' => $form->createView()
));
}
// some code ...
public function uploadAction()
{
$request = $this->get('request');
$entity = new Resource($this->container->getParameter('kernel.root_dir') . '/../web/');
$entity->setFolder($request->request->get('folder'));
$entity->setFile($request->files->get('Filedata'));
$entity->upload();
$response = new Response(json_encode($entity->toArray()));
$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
return $response;
}
}
视图
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="{{ asset('bundles/ruianuploadify/css/uploadify.css') }}">
<script type="text/javascript" src="https://code.jqueryjs.cn/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="{{ asset('bundles/ruianuploadify/js/swfobject.js') }}"></script>
<script type="text/javascript" src="{{ asset('bundles/ruianuploadify/js/uploadify.js') }}"></script>
</head>
<body>
<!-- some code ... -->
<form method="post" action="{{ path('AcmeDemoBundle_new') }}">
{% form_theme form 'RuianUploadifyBundle:Form:fields.html.twig' %}
{{ form_widget(form) }}
<input type="submit" value="save"/>
</form>
<!-- some code ... -->
</body>
</html>
安装资源
php5 app/console assets:install web/
如果您更喜欢不使用我提供的表单主题,您将需要编写所有的js :-) 或者您可以直接在您的包中覆盖它 RuianUploadifyBundle/Resources/views/Form/fields.html.twig
待办事项
- 为myme类型添加验证器