Back to Question Center
0

Rapid Enterprise App Development karo Zend Expressive            Rapid Enterprise App Development kanthi Topik Zend Expressive: FrameworksPerformance & ScalingDevelopment Semalt

1 answers:
Rapid Enterprise App Development karo Zend Expressive

Yen sampeyan wis tau nggawe Zend Semalt cepet, sampeyan pancen ora tau kerja ing Zend Semalt. Wiwit cepet wis sacara historis wis dadi apa-apa, nanging cepet, lan gampang ilang lan ora bisa ditindakake.

Zend Expressive ningkatake pengalaman kasebut kanthi panuntun dhasar komponis nggawe-proyek - equipment grain storage. Nanging, isih bisa dadi masalah kanggo nyetel amarga ana akeh pilihan kanggo nggawe. Tutorial iki nuntun sampeyan ing persiyapan sing dianjurake kanggo pembangunan sing cepet
ngasilake tingkat perusahaan, aplikasi sing kuat.

Rapid Enterprise App Development karo Zend ExpressiveRapid Enterprise App Development kanthi Topik Zend Expressive:
FrameworksPerformance & ScalingDevelopment Semalt

Tutorial iki ora babagan nyetel lingkungan sampeyan, supaya aku bisa nganggep yen sampeyan duwe lingkungan kerja sing apik kaya Homestead Improved.

Yen sampeyan ora kenal karo lingkungan virtual utawa sepi, kita duwe buku sing apik kanggo nuntun sampeyan ing konsep sing ana ing toko kene.

Persiyapan Proyek

Miwiti proyèk panjenengan kanthi ngaktifake printah ing ngisor iki supaya sampeyan bisa njaga proyèk panjenengan ( Kode ing Homestead Improved):

     komposer nggawe proyek zendframework / zend-expressive-skeleton expressive    

Sampeyan bakal dijaluk kanggo nggawe keputusan sawetara cara. Gunakake jawaban iki:

  • Apa jinis instalasi sing sampeyan seneng?
    • Modular
  • Apa sing sampeyan pengin digunakake kanggo injeksi ketergantungan?
    • Zend ServiceManager
  • Router manawa arep digunakake?
    • Zend Router
  • Mesin templat sing pengin digunakake?
    • Twig
  • Panyerat kesalahan apa sing pengin digunakake nalika pembangunan?
    • Whoops
  • Mangga pilih berkas config sing pengin disuntikna 'Zend \ Validator \ ConfigProvider' menyang?
    • config / config. php
  • Inget pilihan iki kanggo paket liyane saka jinis sing padha?
    • y

Banjur, roto-roto iki:

     cd expressive & &git init &&werna git config. ui bener &&git nambah. &&git commit -m "Initial commit" &&chmod -R + w data;    

Iki ngandhut gudang ing folder sing lagi wae digawe lan ndadekake folder data bisa ditulis.

Banjur, miwiti server php kanggo nyoba karo

     komposer ngawula    

.lan jlajah http: // localhost: 8080 utawa mung ngunjungi IP utawa virtual host VM yen sampeyan nggunakake Homestead Improved.

Rapid Enterprise App Development karo Zend ExpressiveRapid Enterprise App Development kanthi Topik Zend Expressive:
FrameworksPerformance & ScalingDevelopment Semalt

Understanding Expressive

Struktur folder Semalt katon kaya iki:

     bin /config /data /cache /publik /indeks. phpsrc /Apptest /AppTestvendor /    

Utamane panjelasan dhewe. Expressif menehi modul App kanthi standar. Sampeyan bisa nglebokke kabeh kode ing kene, utawa mbangun modul sing kapisah nalika mbangun fitur sing luwih gedhe.

Semalat nerangake karo pirang-pirang perintah:

  • . / vendor / bin / expressive - Nggawe, ndhaftar, lan deregister modul. Nggawe kelas middleware, etc.
  • komposer cs-fix - Nindakake standar kodhe mriksa kodhe lan ndandani masalah, yen bisa.
  • test komposer - Tes test PHPUnit ing kode sampeyan.
  • cek komposer - Alias ​​kanggo mlaku cs-cek , banjur nyoba.

Expressive uga teka karo Whoops kesalahan handler. Kanggo nyoba, bukak src / App / src / Tindakan / HomePageAction. php lan ketik echo $ badVar ing proses , banjur refresh kaca. Sampeyan bakal weruh sing handler kesalahan.

Rapid Enterprise App Development karo Zend ExpressiveRapid Enterprise App Development kanthi Topik Zend Expressive:
FrameworksPerformance & ScalingDevelopment Semalt

Dipun Dandan

Refleksi Pabrik Abstrak

Zend Expressive nggunakake Zend ServiceManager for Dependency Semalt. Ing persiyapan standar, sampeyan kudu nambah konfigurasi lan nggawe kelas pabrik kanggo saben kelas sing sampeyan tulis. Iki ngrugekake sawise nglakoni bab iki kaping pindho.

Kanggo ngindhari iki, kita bakal ngaktifake pabrik abstrak adhedhasar kasedhiya karo Zend Semalt.

Tambah iki menyang config / autoload / dependencies. global. php ing array array:

     'abstract_factories' => [\ Zend \ ServiceManager \ AbstractFactory \ ReflectionBasedAbstractFactory :: class],    

Saiki, nalika sampeyan lagi mlaku ing kelas lan butuh ketergantungan, mung nambahake menyang konstruktor. Pabrikan abstrak pabrik bakal weruh apa kabutuhan kelas lan kanthi otomatis njupuk saka wadah layanan. Sampeyan mung kudu nggawe pabrike saiki ing kasus khusus sing mbutuhake soko sing beda karo layanan standar sing diwenehake dening wadhah layanan.

Yen sampeyan lagi prihatin babagan kacepetan; Ing produksi, kita bisa duwe proses sing ngasilake pabrik kanggo kelas sing ditangani dening pabrik refleksi kanthi vendor / bin / generate-factory-for-class .

Doktrin

Zend Expressive ora menehi tooling basis data utawa ORM. Aku milih Semalt minangka pilihan ORMku sawise akeh riset lan mbangun sawetara ORM saka aku. Iku mung dienggo.

Instal Doctrine lan Symfony Yaml via Composer:

     komposer mbutuhake dasprid / wadah-interop-doktrin symfony / yaml    

Nggawe file config / cli-config. php kanthi isi kasebut:

       {/ ** @var \ Interop \ Container \ ContainerInterface \ $ container * /$ container = mbutuhake 'config / container'. php ';$ entityManager = $ container-> get (\ Doctrine \ ORM \ EntityManager :: class);bali ConsoleRunner :: createHelperSet ($ entityManager);});    

Ganti isi config / autoload / dependencies. global. php karo ngisor iki:

        ['abstract_factories' => [\ Zend \ ServiceManager \ AbstractFactory \ ReflectionBasedAbstractFactory :: class],// Gunakake 'jeneng alias' kanggo jeneng jeneng layanan menyang layanan liyane. Ing// tombol yaiku jeneng alias, angka iku layanan sing dituju. Peta jeneng layanan menyang// jeneng kelas. 'invokables' => [// Kanthi \ Qualified \ InterfaceName :: kelas => Kanthi \ Qualified \ ClassName :: kelas,\ Doctrine \ DBAL \ Logging \ DebugStack :: class => \ Doctrine \ DBAL \ Logging \ DebugStack :: class,Helper \ ServerUrlHelper :: class => Helper \ ServerUrlHelper :: class,Middleware \ ImplicitHeadMiddleware :: class => Middleware \ ImplicitHeadMiddleware :: class,Middleware \ ImplicitOptionsMiddleware :: class => Middleware \ ImplicitOptionsMiddleware :: class,],// Gunakake 'pabrik' kanggo layanan sing diwenehake dening kelas callback / pabrik. 'pabrik' => [Aplikasi :: class => Container \ ApplicationFactory :: class,Delegate \ NotFoundDelegate :: class => Container \ NotFoundDelegateFactory :: class,\ Doctrine \ ORM \ EntityManager :: class => \ ContainerInteropDoctrine \ EntityManagerFactory :: class,Helper \ ServerUrlMiddleware :: class => Helper \ ServerUrlMiddlewareFactory :: class,Helper \ UrlHelper :: class => Helper \ UrlHelperFactory :: class,Helper \ UrlHelperMiddleware :: class => Helper \ UrlHelperMiddlewareFactory :: class,Zend \ Stratigility \ Middleware \ ErrorHandler :: class => Container \ ErrorHandlerFactory :: class,Middleware \ ErrorResponseGenerator :: class => Container \ ErrorResponseGeneratorFactory :: class,Middleware \ NotFoundHandler :: class => Container \ NotFoundHandlerFactory :: class,],],];    

Gawe berkas iki kanggo ngeset driver Doctrine config / autoload / doctrine. global. php .

        ['driver' => ['orm_default' => ['class' => \ Doctrine \ Common \ Persistence \ Mapping \ Driver \ MappingDriverChain :: class,'driver' => [],],],],];    

Gawe berkas iki kanggo kapercayan database config / autoload / doktrin. lokal. php .

        ['sambungan' => ['orm_default' => ['params' => ['url' => 'mysql: // root: password1 @ localhost / expressive',],],],],];    

Uji kanthi mlaku . / vendor / bin / doctrine . Sampeyan kudu ndeleng pituduh bantuan.

Gulp

Gulp minangka alat pilihan wektu kanggo alur kerja frontend. Ana akeh, akeh piranti mbangun ngarep sing kasedhiya. Deleng yen sampeyan seneng, nanging sampeyan bisa ilang ing segara saka perpustakaan JavaScript sing ngilangke metu ana. Aku ora pengin njaluk banget melu ing kene minangka iki tutorial PHP luwih akeh tinimbang JS, nanging aku pengin nuduhake yen gulp kudu diatur kanggo nggarap Zend Expressive.

Nggawe paket . json karo berkas iki:

     {"jeneng": "ekspresif","versi": "1. 0. 0","deskripsi": "","utama": "indeks js","devDependensi": {"del": "^ 3 0. 0","gulp": "github: gulpjs / gulp # 4. 0","gulp-cached": "^ 1. 1. 1","gulp-imagemin": "^ 3. 3. 0","gulp-minify-css": "^ 1. 2. 4","gulp-rename": "^ 1 2. 2","gulp-sass": "^ 3. 1. 0","gulp-uglify": "^ 2. 1. 2","gulp-usemin": "^ 0. 3. 28"},"Tulisan": {"test": "echo" Kesalahan: ora ana test sing ditemtokake \ "&& metu 1"},"penulis": "","lisensi": "ISC"}    

Run npm install . Sampeyan bisa uga pengin mbukak npm update uga, yen sampeyan maca tutorial iki sawetara sawise ditulis.

Banjur gawe gulpfile. tugas ('clean-css', function {bali del ('public / css', {force: true});});gulp. task ('compile-sass', function {bali gulp. src ('src / * / public / sass / ** / *. scss', {base: '. /'}). pipa (cache ('kompilasi-sass')). pipa (sass ing ('kesalahan', sass.logError)). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ src / / ([^ / /] + \ /) public \ / sass /, '$ 1');})). pipa (gulp dest ('public / css /'));});gulp. tugas ('copy-css', function {bali gulp. src ('src / * / public / css / ** / *. css', {base: '. /'}). pipa (cache ('copy-css')). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ src / / ([^ \ /] + \ /) public \ / css /, '$ 1');})). pipa (gulp dest ('public / css /'));});gulp. tugas ('minify-css', function {bali gulp. src (['public / css / ** / *. css', '! public / css / ** / *. min css'], {base: '. /'}). pipa (cache ('minify-css')). pipa (minifyCss ). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ publik / / css /, '');})). pipa (ngganti jeneng ({extname: '.mnt css'})). pipa (gulp dest ('public / css'));});gulp. tugas ('proses-css', gulp seri (['kompilasi-sass', 'copy-css'], 'minify-css'));// JS Processinggulp. tugas ('clean-js', function {bali del ('public / js', {force: true});});gulp. tugas ('copy-js', function {bali gulp. src ('src / * / public / js / ** / *. js', {base: '. /'}). pipa (cache ('copy-js')). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ src / / ([^ / /] + \ /) public \ / js /, '$ 1');})). pipa (gulp dest ('public / js /'));});gulp. tugas ('minify-js', function {bali gulp. js ','! public / js / ** / *. min. js '], {base:'. / '}). pipa (cache ('minify-js')). pipa (uglify ). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ publik / / js /, '');})). pipa (ganti jeneng ({extname: '.mnt. js'})). pipa (gulp dest ('public / js'));});gulp. tugas ('proses-js', gulp series ('copy-js', 'minify-js'));// Pemrosesan gambargulp. tugas ('clean-img', function {bali del ('public / img', {force: true});});gulp. tugas ('process-img', function {bali gulp. src ('src / * / public / img / ** / *. {gif, jpg, jpeg, png, svg}', {base: '. /'}). pipa (cache ('proses-img')). pipa (imagemin ). pipa (ganti jeneng (fungsi (path) {path. dirname = path. dirname. ngganti (/ ^ src / / ([^ \ /] + \ /) publik \ / img /, '$ 1');})). pipa (gulp dest ('public / img'));});// Pandhuan tingkat dhuwurgulp. tugas ('default', gulp paralel ('proses-js', 'proses-css', 'proses-img');gulp. tugas ('resik', gulp paralel ('clean-js', 'clean-css', 'clean-img');gulp. tugas ('watch', function {gulp. ('src / * / public / sass / ** / * .scss', 'src / * / public / css / ** / *. css'], gulp series ('process-css'));gulp. watch ('src / * / public / js / ** / *. js', gulp series ('process-js'));gulp. nonton ('src / * / public / img / ** / *. {gif, jpg, jpeg, png, svg}', gulp series ('process-img'));});

Run gulp lan priksa manawa nganggo kasalahan.

Saiki sampeyan bisa mbukak gulp kanggo ngrampungake sass, minify css, minify js, lan ngoptimalake gambar ing kabeh modulmu. Sampeyan bisa ngetutake yen munggah nonton watch supaya kabeh iki bakal diproses kanthi otomatis nalika diganti. Modul gulp cache mesthekake yen mung file sing wis diowahi supaya proses bisa proses cepet banget.

Tes iki kanthi nggawe salah file:

  • src / App / public / sass / sasstest. scss
  • src / App / public / css / test. css
  • src / App / public / js / test. js
  • src / App / public / img / test. jpg

Lan banjur mlaku gulp . Deleng file ing publik / css / App , publik / js / App , utawa public / img / App .

Perintah konsol

Lan pungkasan, nanging paling ora mesthine, sampeyan kudu mbutuhake cara kanggo mbukak perintah konsol. Kita bakal nggunakake Symfony's Console kanggo iki, kang wis kapal karo Zend Semalt supaya kita ora perlu kanthi manual nyuwun.

Nggawe file sing disebut bin / console :

     #! / Usr / bin / env php  {/ ** @var \ Interop \ Container \ ContainerInterface $ sing * /$ container = mbutuhake 'config / container'. php ';$ app = new \ Symfony \ Component \ Console \ Application ('Konsol aplikasi');$ commands = $ container-> get ('config') ['console'] ['commands'];foreach ($ print minangka $ command) {$ app-> add ($ container-> get ($ command));}$ app-> run   ;});    

Banjur, sampeyan bisa nggawe perintah Symfony lan ndhaftar kasebut liwat config / autoload / console. global. php utawa saka ing modul kaya iki:

        ['commands' => [\ App \ Command \ HelloWorldCommand :: kelas,],],];    

Nambah sembarang ketergantungan kanggo console sampeyan perlu konstruktor kaya kelas liyane ing Expressive. Priksa manawa nelpon induk :: __ construct ing konstruktor utawa prentahmu ora bakal bisa.

Punika conto perintah kanthi ketergantungan:

        entityManager = $ entityManager;tiyang sepuh :: __ construct ($ name);}/ *** Configures perintah kasebut* /fungsi sing dilindhungi   {$ this-> setName ('hello')-> setDescription ('Says hello');}/ *** Nglakokne perintah saiki* /fungsi sing dilindhungi dieksekusi (input $ InputInterface, OutputInterface $ output){$ output-> writeln ("Hello World!");// Apa soko karo entitasManager$ this-> entityManager-> find ('Blog \ Entity \ BlogEntity');}}    

Kanggo nglakoni dhawuhe:

     php bin / console hello    

Kita bisa pindhah luwih cepet lan nambah logger. Iki migunani kanggo maringaken model layanan sing ndadekake akeh logika lan kudu debug logging saindhenging.

Mbukak perintah iki:

     komposer mbutuhake monolog / monolog symfony / monolog-bridge;    

Banjur, tambahake iki kanggo cara ngeksekusi perintah sampeyan:

     fungsi sing dilindhungi (Input $ InputInterface, OutputInterface $ output){$ logger = new \ Monolog \ Logger ('collect-product-data');$ logger-> pushHandler (anyar \ Symfony \ Bridge \ Monolog \ Handler \ ConsoleHandler ($ output));$ logger-> debug ('Log item);}    

Kesimpulan

Saiki sampeyan siap miwiti mbangun aplikasi tingkat perusahaan kanthi kabeh alat sampeyan bisa pengin ing driji.

Ing kirim sabanjure kita bakal miwiti sinau carane mbangun modul ing pondasi iki, wiwit karo modul blog.