Pustaka komponen visual (JQuery UI, Twitter Bootstrap, Semantic-UI) untuk kerangka kerja php dan php MVC
situs web phpMv-UI
phpMv-UI adalah pustaka komponen visual untuk php : pembungkus php untuk komponen jQuery dan UI (jQuery, Twitter Bootstrap, Semantic-UI).
Dengan menggunakan injeksi ketergantungan, objek jQuery dapat disuntikkan ke dalam wadah kerangka php , memungkinkan pembuatan skrip jQuery di pengontrol, dengan menghormati pola desain MVC.
Instal Komposer di lokasi umum atau di proyek Anda:
curl -s http://getcomposer.org/installer | php
Buat file composer.json di direktori aplikasi sebagai berikut:
{
"require" : {
"phpmv/php-mv-ui" : " ^2.3 "
}
}
Di direktori aplikasi, jalankan penginstal komposer :
php composer.phar install
Cukup kloning repositori di lokasi umum atau di dalam proyek Anda:
git clone https://github.com/phpMv/phpMv-UI.git
phpMv-UI mematuhi rekomendasi PSR-4 untuk kelas pemuatan otomatis. Apapun framework php yang digunakan, dengan "composer" cukup mengintegrasikan file autoload Composer.
require_once ( " vendor/autoload.php " );
Pustaka sudah dimuat secara default di file konfigurasi app/config/config.php :
" di " => array (
" @exec " => array ( " jquery " => function ( $ controller ){
return Ajax php ubiquity JsUtils:: diSemantic ( $ controller );
})
),
Contoh pembuatan tombol Semantic-UI
/**
* @property AjaxphpubiquityJsUtils $jquery
*/
class ExempleController extends Controller{
public function index (){
$ semantic = $ this -> jquery -> semantic ();
$ button = $ semantic -> htmlButton ( " btTest " , " Test Button " );
echo $ button ;
}
}
Tanpa Composer, dimungkinkan untuk memuat perpustakaan dengan file app/config/loader.php :
$ loader = new Phalcon Loader ();
$ loader -> registerNamespaces ( array (
' Ajax ' => __DIR__ . ' /../vendor/phpmv/php-mv-ui/Ajax/ '
))-> register ();
Penting untuk menyuntikkan layanan JQuery saat startup aplikasi, di file layanan app/config/services.php , dan jika perlu membuat instance Semantic, Bootstrap atau Jquery-ui :
$ di -> set ( " jquery " , function (){
$ jquery = new Ajax php phalcon JsUtils ();
$ jquery -> semantic ( new Ajax Semantic ()); //for Semantic UI
return $ jquery ;
});
Contoh pembuatan tombol Semantic-UI
use Phalcon Mvc Controller ;
use Ajax php phalcon JsUtils ;
/**
* @property JsUtils $jquery
*/
class ExempleController extends Controller{
public function indexAction (){
$ semantic = $ this -> jquery -> semantic ();
$ button = $ semantic -> htmlButton ( " btTest " , " Test Button " );
echo $ button ;
}
}
Jika Anda ingin CodeIgniter menggunakan pemuat otomatis Komposer, cukup atur $config['composer_autoload']
ke TRUE
atau jalur khusus di application/config/config.php .
Kemudian, perlu membuat perpustakaan untuk kelas JsUtils
Buat perpustakaan XsUtils (namanya gratis) di folder aplikasi/perpustakaan
use Ajax php ci JsUtils ;
class XsUtils extends Ajax php ci JsUtils{
public function __construct (){
parent :: __construct ([ " semantic " => true , " debug " => false ]);
}
}
Tambahkan pemuatan perpustakaan XsUtils di file application/config/autoload.php
Anggota jquery akan dapat diakses di pengontrol
$ autoload [ ' libraries ' ] = array ( ' XsUtils ' => ' jquery ' );
Setelah dimuat, Anda dapat mengakses kelas Anda di pengontrol menggunakan anggota $jquery :
$ this -> jquery -> some_method ();
Jika Anda tidak menggunakan file autoloader Composer, Anda juga dapat memuat phpMv-UI dengan composer.json :
"autoload" : {
"classmap" : [
...
],
"psr-4" : {
"Ajax \ " : " vendor/phpmv/php-mv-ui/Ajax "
}
},
Daftarkan Singleton di file bootstrap/app.php :
$ app -> singleton ( Ajax php laravel JsUtils::class, function ( $ app ){
$ result = new Ajax php laravel JsUtils ();
$ result -> semantic ( new Ajax Semantic ());
return $ result ;
});
Kemudian dimungkinkan untuk menyuntikkan kelas JsUtils ke konstruktor pengontrol kelas dasar:
use Ajax php laravel JsUtils ;
class Controller extends BaseController{
use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
protected $ jquery ;
public function __construct ( JsUtils $ js ){
$ this -> jquery = $ js ;
}
public function getJquery () {
return $ this -> jquery ;
}
}
Kelas-kelas dalam paket Komposer yang terinstal dapat dimuat secara otomatis menggunakan pemuat otomatis Komposer. Pastikan skrip entri aplikasi Anda berisi baris berikut untuk menginstal autoloader Komposer:
require ( __DIR__ . ' /../vendor/autoload.php ' );
require ( __DIR__ . ' /../vendor/yiisoft/yii2/Yii.php ' );
Dalam file yang sama, daftarkan ketergantungan baru:
Yii:: $ container -> setSingleton ( " AjaxphpyiiJsUtils " ,[ " bootstrap " => new Ajax Semantic ()]);
Singleton JsUtils kemudian dapat disuntikkan ke pengontrol
namespace app controllers ;
use yii web Controller ;
use Ajax php yii JsUtils ;
class SiteController extends Controller{
protected $ jquery ;
public function __construct ( $ id , $ module , JsUtils $ js ){
parent :: __construct ( $ id , $ module );
$ this -> jquery = $ js ;
}
}
Jika Anda tidak menggunakan file autoloader Composer, Anda juga dapat memuat phpMv-UI dengan Ps4ClassLoader :
use Symfony Component ClassLoader Psr4ClassLoader ;
require __DIR__ . ' /lib/ClassLoader/Psr4ClassLoader.php ' ;
$ loader = new Psr4ClassLoader ();
$ loader -> addPrefix ( ' Ajax \' , __DIR__ . ' /lib/phpmv/php-mv-ui/Ajax ' );
$ loader -> register ();
Buat layanan yang mewarisi dari JquerySemantic
namespace App Services semantic ;
use Ajax php symfony JquerySemantic ;
class SemanticGui extends JquerySemantic{
}
Periksa apakah pengkabelan otomatis diaktifkan di config/services.yml :
services :
# default configuration for services in *this* file
_defaults :
autowire : true # Automatically injects dependencies in your services.
Anda kemudian dapat menggunakan injeksi ketergantungan pada properti, konstruktor, atau penyetel:
namespace App Controller ;
use Symfony Bundle FrameworkBundle Controller AbstractController ;
use App Services semantic SemanticGui ;
BarController extends AbstractController{
/**
* @var SemanticGui
*/
protected $ gui ;
public function loadViewWithAjaxButtonAction (){
$ bt = $ this -> gui -> semantic ()-> htmlButton ( ' button1 ' , ' a button ' );
$ bt -> getOnClick ( " /url " , ' #responseElement ' );
return $ this -> gui -> renderView ( " barView.html.twig " );
}
}
Buat 2 layanan di file app/config/services.yml :
parameters :
jquery.params :
semantic : true
services :
jquery :
class : AjaxphpsymfonyJsUtils
arguments : [%jquery.params%,'@router']
scope : request
app.default_controller :
class : AppBundleControllerDefaultController
arguments : ['@service_container','@jquery']
Kemudian dimungkinkan untuk memasukkan container Symfony dan layanan JsUtils ke dalam konstruktor pengontrol :
namespace AppBundle Controller ;
use Sensio Bundle FrameworkExtraBundle Configuration Route ;
use Symfony Bundle FrameworkBundle Controller Controller ;
use Symfony Component DependencyInjection ContainerInterface ;
use Ajax php symfony JsUtils ;
use AppBundle AppBundle ;
/**
* @Route(service="app.default_controller")
*/
class DefaultController extends Controller{
/**
* @var AjaxphpsymfonyJsUtils
*/
protected $ jquery ;
public function __construct ( ContainerInterface $ container , JsUtils $ js ){
$ this -> container = $ container ;
$ this -> jquery = $ js ;
}
}
Salin file JsUtilsComponent.php yang terletak di vendor/phpmv/php-mv-ui/Ajax/php/cakephp ke folder src/controller/component proyek Anda
Tambahkan pemuatan komponen JsUtils dalam metode inisialisasi pengontrol dasar AppController , yang terletak di src/controller/appController.php
public function initialize (){
parent :: initialize ();
$ this -> loadComponent ( ' RequestHandler ' );
$ this -> loadComponent ( ' Flash ' );
$ this -> loadComponent ( ' JsUtils ' ,[ " semantic " => true ]);
}
objek jquery di pengontrol akan dapat diakses di $this->JsUtils->jquery
Pada sebagian besar IDE (seperti Eclipse atau phpStorm), untuk mendapatkan penyelesaian kode pada instance $jquery
, Anda harus menambahkan properti berikut dalam dokumentasi pengontrol:
/**
* @property AjaxJsUtils $jquery
*/
class MyController{
}