Vous êtes sur la page 1sur 4

DevelopersClubgeekdailyblog

Home(/)

Categories(/categories/)

Companies(/companies/)

SlimframeworkoraswehaverefusedCMS
PHP(/categories/php/) CMS(/categories/cms/)

1year,8monthsago

ThispublicationwillbeinterestingtothesmallcompaniesondevelopmentofthesiteswithuseofCMS.Small
background:weitissmalldepartmentwhichisengagedindevelopmentofwebsites.Verylongtimewedeveloped
thesitesonlyonmanagementsystems(OpenCart,WordPress,MODXandsamopisny)whichinessenceweresimple
andsame,butsincesummeroflastyearthetrendhascometodevelopmentofthesiteswhichnotsimplywereonthe
Internetfaceofthecompany,butoneofinstrumentsofbusiness.
So,forexample,therewasorderfortransportcompanywhichcarriesouttriproutesandwouldlikethatall
management(armoringandpaymentoftheticket,includingthroughthecashier)occurredthroughthesite.
Asyoucanguess,hereitisnotnecessarytowaitforbigadvantageofCMS.Evenonthecontraryanyalgorithmto
whichitfollowed,disturbedus.Insomedaysonthefirstsuchsitetherewereonecrutchesandpracticallyall
processingwentonlyonjavascript,andCMSwasusedaslayerforrecordandselectionofdatabase.
Suffersuchwelongcouldnotand,havingconferred,havedecidedthatforsuchprojectswewillusemicroframeworkandhaveatoncetakennoticeofSlim.
Astheofficialsitesays:
Slimismicroframeworkwhichwillhelpyoutowritequicklysimple,butpowerfulwebapplications.

Andthisistrue,youcandownloaditmanuallyarchiveoruseComposer.Afterthatcreateindex.phpwiththefollowingcontents:

//
require'Slim/Slim.php';

//
\Slim\Slim::registerAutoloader();

$app=new\Slim\Slim();
$app>get('/hello/:name',function($name){

echo",$name";

});
$app>run();

Thissimpleapplicationwhichdisplayshi{{username}}upontransitiontourl.
ButaswehavebeendisappointedthatonitthemajorityofarticlesontheInternetcomestoanendandatthisstagealotofthingsremainforusunclear.
Slimdoesnotsetaccuratesystemofdistributionsoffilesondiskthereforewehavenotfoundthebestway,thantoiklyuditeachrouting.Nextday,havingunderstoodthat
somethinggoesnotso,hasbeendecidedtocreatetheseparatefolderandtoputtherefilesongroups.Thefollowingwasaddedtotheindex.phpfile

foreach(glob('Routes/*.php')as$file)

include($file);

Overtimeeachsuchfilebecametoobig,buthavinglookedatofficialdocumentation(http://docs.slimframework.com/),havefoundnothing.Thesolutionhasbeenfoundon

githubwhereithasbeentoldthatitisnecessarytodo:

$app>get('post/:type/:id','\Controller\Post:onePost');

Where\Controller\PostclasswhichasIunderstand,isaddedaccordingtoRSP0.AndonePostisitsmethod.
ThusourfilestructurewasupdatedonemoredirectoryofControllerinwhichwewroteactionswhichcoulduseatonceinseveraloptionsofrouting.Iconsiderasgood
examplethisroutingwhichisputintogroup,stylewhichisforsomereasonnotdescribedinthesamedocumentation.

$app>group('/',function()use($app){

$app>get('post/:type/:id','\Controller\Post:onePost')

>conditions(

array('type'=>'[azAZ]{3,}','id'=>'[09]{1,}')

>setParams(

array('app'=>$app,'type'=>$type=NULL,'id'=>$id=0)

>name('onePost');
});

Whereconditionsvalidationonregularexpression,setParamsparameterpassingtotheonePostmethod,andnamenameforpossibleredirectonthispage(For
example$app>redirectTo('onePost'))
Notsuperfluouswillcarryoutinspectiononavailabilitynotinthecontroler,andatthelevelofroutig,thanksto"Middleware".Forexample,fortheadministrator'spanelatonce
itispossibletoputonthewholegroupsuchas:

$app>group('/admin',$authenticateForRole($app,'admin'),function()use($app){
...
});

Wherethesecondargumentistheappropriatedanonymousfunctionofvariable.Inmycaseitlooksthus:

$authenticateForRole=function($app,$role='admin'){

returnfunction()use($app,$role){

\Controller\Autch::chekUserRole($app,$role);

};
};

Unfortunately,andforsome,maybe,andfortunately,inslimthereisnobuiltinworkwithdatabase,butthisissueisrathersimplyresolvedwhenwecreateobjectofApp.We
cantransfersomeargumentswhichwillbeusedbyit.
Forexample:

$app=new\Slim\Slim(array(

'templates.path'=>'Views',

'debug'=>TRUE,

'mode'=>'development',

'BDbase'=>'localhost',

'BDuser'=>'username',

'BDpassword'=>'password',

'BDname'=>'basename'
));

AfterthatwewillbeablequietlytouseDependencyInjectionforworkwithdatabase,addressingtotheparameterstransmittedatcreationbymeansofmethodof$appconfig
('Name')

$app>database=newmysqli(

$app>config('BDbase'),

$app>config('BDuser'),

$app>config('BDpassword'),

$app>config('BDname')
);
if($app>database>connect_errno){

echo"MySQL:(".$app>database>connect_errno.")".$app>database>connect_error;

die();
}

$app>database>query("SETNAMES'utf8'");
$app>database>query("SETCHARACTERSET'utf8'");
$app>database>query("SETSESSIONcollation_connection='utf8_general_ci'");

AllotheropportunitiesofSlimareratherwelldescribedindocumentation.IntheconclusionIwanttotellthatforourteamwhichneverworkedwithdifferentframrwork'ami
earlier,itwasgoodexperienceofwhichwealreadymakeuseinprojects.ToreturntoanyCMSofdesiredoesnotarise.

Thisarticleisatranslationoftheoriginalpostathabrahabr.ru/post/251881/
Ifyouhaveanyquestionsregardingthematerialcoveredinthearticleabove,please,contacttheoriginalauthorofthepost.
Ifyouhaveanycomplaintsaboutthisarticleoryouwantthisarticletobedeleted,please,dropanemailhere:
sysmagazine.com@gmail.com(mailto:sysmagazine.com@gmail.com).
Webelievethattheknowledge,whichisavailableatthemostpopularRussianITbloghabrahabr.ru,shouldbeaccessedbyeveryone,eventhoughitispoorly
translated.
Sharedknowledgemakestheworldbetter.
Bestwishes.

0Comments
Recommend

sysmagazine

Share

Login

SortbyBest

Startthediscussion

Bethefirsttocomment.

ALSOONSYSMAGAZINE

DevelopmentofAndroidofapplicationforworkwithOBDIIthe
protocol

WewritetacticalgameaboutdigitsunderAndroidITdailyblog,
news,magazine,technologies

1comment8monthsago

1comment6monthsago

hoangvuThankyousomuchforyourpost.HaveyougettheDistance(

icthereisanarticleaboutpoliciesandrules,whichIhavetorespectifI

MIL)bythissourcecodethatsupportDistanceCommand?Idonotwantto
getdistancebyGPS...

amgoingtocreateownappsandpublishitingooglestore?Ashortandgood
summery:)thanks

NewPhpStorm7:outsideoflanguageITdailyblog,news,
magazine,technologies

Xamarin.ProsandconsITdailyblog,news,magazine,
technologies

1comment9monthsago

1comment8monthsago

clodIliketouseCodelobstermuchmore:http://www.codelobster.com

Glorium Aninterestinginfographicthatillustratesthemainfeaturesof

Xamarin:http://gloriumtech.com/blog/xa...

Subscribe d AddDisqustoyoursiteAddDisqusAdd

Privacy

Vous aimerez peut-être aussi