Download the PHP package tahona/spark-mvc without Composer
On this page you can find all versions of the php package tahona/spark-mvc. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tahona/spark-mvc
More information about tahona/spark-mvc
Files in tahona/spark-mvc
Package spark-mvc
Short Description Web MVC Framework
License MIT
Homepage https://github.com/primosz67/spark-spark
Informations about the package spark-mvc
README
What is this repository for?
Very fast PHP framework with autoloading and annotations. All configuration is handled by annotations and beans.
How it works
Framework automatically load beans from app/src path it just need to be annotated with one annotation of: @Component, @Service, @Configuration or @Repository.
Bean injection can be achieved with @Inject annotation.
Initialization is done on the first request. The second request execute Controllers action, that has already injected beans in it.
Request execution time is very small because it's time of user code execution only.
Thanks to this, for real production code we got 20ms-40ms per request for dynamic data.
Quick Start
For quick start donwload example project: https://github.com/primosz67/spark-mvc-example
Index php - explained
app/public/index.php
Note: If you will use doctrine db framework add here line - "AnnotationRegistry::registerLoader('class_exists');"
Framework setup:
Configuration
app/src/MyAppConfig.php
- resetParam - parameter for clearing cache. (GET http://app.com?reset). It's for development environment and should be deleted for production.
Controller
1. Standard Controller with annotation controller
app/src/MyAppController.php
Go to localhost/get or localhost/index;
- Request is injected to method by Type
- SomeForm is filled by "POST" form params using "spark-form" module.
- $beanName - framework can inject any bean or service needed for action by name or type.
2. Rest Controller
Note: All methods in RestController will resolve to JSON.
SomeResultDto - will resolve to json.
3. Old method of creating Controller
app/src/MyAppController.php
Go to localhost/get or localhost/index;
2. Define bean for autoload.
Injection
1. Define bean for autoload.
Note: for enabled apcu @EnableApcuBeanCache("reset") to autoload injection, call http://website?reset
2. Define other beans and inject out bean.
3.Inject in controller
4.Inject in action method of controller
View
apc/view/{controller package}/{controllerName (without "Controller")}/{action}.tpl
- For app/src/MyAppController@showNewViewAction we get: apc/view/myapp/showNewView.tpl
- For app/src/some/serious/package/controller/MyAppController@showNewViewAction we get: apc/view/some/serious/package/myapp/showNewView.tpl
Keywords action and controller are deleted by default.
Smarty built-in path plugins
Function path can be used in tpl file (example: index.tpl)
Command will resolve to : www.example.com/user/register
Command will resolve to : www.example.com/user/register
- com.example.RegisterController - optional Controller class Name
- registerAction - method name in RegisterController
Command will resolve to : www.example.com/blogpost/remove/4
- {controller} - current request controller will be used
- removeAction - method name in RegisterController
- id:4,type:blogpost - params will resolve /{type}/remove/{id} to it's final form
Apcu Bean Cache
if @EnableApcuBeanCache annotation is added with @Configuration the only way to reset beans and init them once more is by requestin localhost:80?reset (GET parameter "reset").
Mailer
- @EnableMailer -TODO
- spark.mailer.enabled (true/false)- property
@Annotations
The heart of Spark Framework.
- @Component,@Service,@Repository,@Configuration - do the same thing, but purpose is the key.
- @Bean
- @PostConstruct
- @Inject
- @Bean
Application Parameters
$this->config
Base parameters: app.path - pierwsza ścieżka do katalogu /app src.path - ścieżka do katalogu /app/src app.paths - ścieżka albo ścieżki do katalogu /app w różnych kontekstach
to fetch parameters:
update or set Params
Custom module loading
If you create common module to use in other project remember to create beans by @Bean annotation. It will be easier to add new module in one go.
In some your app configuration add others OtherModuleConfig.
All @Bean annotation in OtherModuleConfig will be created and inject to your classes.
Multiple DataBase connection (example)
Handle multiple connections. To create Doctrine's EntityManager you can use simple EntityManagerFactory.
Note: for using CrudDao with other enityManager than basic use @OverrideInject annotation
Internalization
Bean definition
Where "pl","cz","en" are cookie value with key "lang";
-
Property file /house/house_pl.properties
-
Use in php
- Use in smarty
Results: Thank You John and Trevor
@Path
Annotation Defonition
Fetch path paramether in Controller class;
MULTI fetch by component
Example is for dynamic menu module that will popup when new project or classes are added.
Interceptors
Command behaviour for PHP cli
First, create class Command implementation
in console execute:
Output:
Built-in cache service
Great thing for caching DB request or loading files data. Annotation can be used with different cache. Even custom cache bean that implement spark/cache/Cache
How To
In Bean class add @Cache annotation.
- "name" is a name of bean that implement spark\cache\Cache interface.
- ApcCache needed for application is added as default name="cache"
- "time" - optional parameter that is in minutes(10 minutes)
- "key" parameter is for distinguish cached values
Profiles
In this case SomeDevelopmentConfig won't be added to container and bean declared in it (@Bean) as well.
Error handling - example
where error handler with order equal 0 , will be first to invoke. If you return Viewmodel the handling will stop and the view will be return as response.
Event Bus
Event definition :
Subscription
AnnotationHandler
Add Your own annotation
Installation - Composer - Speed up
Performance - Some numbers
Tested Case: Real project with small database. AB(Apache benchmark) requests 10000 and 1000 parallel connections.
- Smarty compile: Smarty template rendering for each request (Development mode).
- Smarty: smarty with production setup.
- Smarty + @Cache (Redis): request to database are cached with @Cache annotation (we use Redis for caching). It can give big improvement to performance when there is more calls to DB.
Mode | Request time (per request) |
---|---|
Smarty compile | ~130ms |
Smarty | ~45ms |
Smarty + @Cache (Redis) | ~25ms |