I need create a lot functions, used by 6+ Controllers. So... shared functions:
This functions needs:
Model Access
Database Access
Lib Access
Vendor Access
Plugin Access
View Redirect
Return parameters (callback)
What the best way?
Lib
Component
Behavior
Vendor
My website configuration:
CakePHP 2.3
MySQL 5.5
PHP 5.3.9
Apache 2.2
Sorry for my english.
Thanks.
Helpers are for (multiple) views
Behaviors are for (multiple) models
Tasks are for (multiple) shells
Components are for (multiple) controllers
So - if you share those functions across controllers, the logical answer would be to use components.
All the rest of your "options" provided fail for at least one reason.
But keep in mind that you still have to keep the model stuff close to the model layer and should only use it via model methods in the controller/components.
Don't make those components a powerful model or something. They are meant to share "logic" between controllers.
Simply you can write those function in Appcontroller file so you can access them globally.
Related
I'm beginner at Lumen (and php) and I am a bit confused all the structure as the only people I talked about Lumen to were interns just like me. I created a project where I use Models where all my methods and functions are, I have constructor in my controller class where I call functions from class Model.php and then pass that to view.blade.php (in resources folder) where all my html goes. I return view from controller. My question, is that correct? Should I have class for view? If my structure is not correct, how should it look? What correct sequence of passing information should be then?
Thank you in advance for claryfing it for me :)
I know MVC structure should be similar to .NET but still I am confused somehow.
You should not use Lumen for a fully-fledged application with HTML views, assets, etcetera.
Lumen is a microframework specifically designed to build APIs in a lightweight environment, which allows you to focus entirely on the resources to serve, instead of the design.
Lumen is way faster compared to Laravel, for example the Routing is handled by FastRoute instead of Illuminate Routing, which is quicker and lighter for the application.
Use Laravel for what you are trying to do.
Before using Symfony2, I used to have a common lib with a lot of simple but useful functions (for instance, a function which takes "Azè_rtï" in argument and returns "aze-rti").
So, well, I created a Bundle: CommonLibsBundle.
But.. I have only one or two php files. It does not make sense to me to use a controller / view / model in this kind of situation.
What should I do? May I erase all folders in my new bundle (Controller, DependencyInjection, Resources, Tests... + CommonLibsBundle.php) and just put my lib.php in it?
Many thanks,
Bliss
Unless you need to tap into the Symfony framework itself - for configuration or to define services, it doesn't need to be a bundle - it's just a library. Give it a reasonable namespace, and call as required as you would any other component or library.
Even if you wanted to add Symfony-specific services that you could call, there is something to be said to still have an external simple library - usable anywhere, which then is wrapped by a very thin bundle which would only add the Symfony-specific (or Laravel, or ZF, or whatever) services and configuration as required.
I have two apps running at same server and one of them is CI. I need the one that is not CI to use models, libraries, helpers etc. So I needed something like get_instance(), but I'm not in a controller's context, I'm outside CodeIgniter.
How can I get a generic instance of a controller, or instantiate models or import helpers from a different application?
Example:
A and CI are apps and both runs on the same server
ROOT
A
script.php
CI (it's a CodeIgniter project, with controllers, models etc)
...
random_model.php
...
random_helper.php
How can I make script.php on A use random_model.php on CI?
Nice question, but I'm afraid that you can't do that, and even if we assume you can do it, this will do more damage than good, for example you call a custom library that is using the session library, then your application "A" will implicitly use that session library and can have unpredictable consequences,mostly if "A" is using native php session.
This is not only true for session but other configuration settings too (database, base_url,charset,...and other settings) and it will auto load other libraries, helpers, packages ...etc if any from your autoload.php
a better way is to create a controller under the "codeigniter" project and call it as you usually do with other controllers.
or even better, create a second codeigniter project, since it has a very small footprint, for your script "A" and avoid all conflict between the two projects and get more organized
I hope that makes sense.
Is there a way to use Doctrine using the model classes I've already setup for my Symfony applications without having to call Symfony with all that overhead?
This is more to satisfy a curiosity than anything else. For all the scripts I've used, I've just been able to instantiate Symfony (which typically turns out nice since I have all of the features that I'm used to working with on this particular project. But there has to be a way to load Doctrine and use the Symfony model classes without Symfony... Right?
Doctrine isn't dependet on symfony. Doctrine is a "framework" on its own. It has it's own autoloading and can therefore work with it's classes like a regular PHP app. You can integrate Doctrine with other frameworks if you want (like CodeIgniter or Zend). So you have every freedom you need without the need to do some tedious migration of your models/data/... from one system to another.
I've come to the conclusion there really isn't a way to use the model classes from Symfony elsewhere. With a little work, you can port over the classes to a new Doctrine model (even if you use the generator, since the main model class just extends the base which extends sfDoctrineRecord (from the API docs, you can see which functions will need to be removed).
Otherwise, there isn't a practical way of doing that.
Anytime I need to access the Symfony model, I'm making a task or plugin since I do typically need part of Symfony's functionality.
As far as Symfony2 goes, just looking at the documentation makes me want to run screaming. It's not mature in any form or fashion (but, then again, neither is Symfony "legacy"). So, I'm not sure if the process would be any easier there.
The challenge:
I want to modularize my library folder in my zend framework app. This is fine, if you want to put everything in the same namespace such "App_." But the problem comes in when you have a dozen packages such as a SignUp package, ACL Package, Navigation Package, Foo Package etc. Now each packages has some view helpers, some controller plugins, some action helpers plus some other base classes. You could add each view helper path individually, but that could junk up your application.ini file/ bootstrap.
So the question is, is anyone aware of 'Plugin Package' loader for ZF?
To clarify, it would be nice to have a resource plugin that you pass the package name, it adds the namespace, registers some default options like helper paths and then you can configure it to add helpers to Action Helper brokers. Each plugin package might have to have its own ini file or an init class that where the programmer could initialize the plugin package. Any thoughts or knowledge of something like this would be appreciated.
Your question is difficult to find a solution to. ZF doesn't seem to be used in the manner you wish.
For example you want the following packages
Navigation
ACL
Sign Up
These are all completely separate and don't all 'plugin' to ZF in a similar manner.
Navigation needs to be stored and built for each request that needs the package, the navigation object then needs to be used in Zend_View objects or perhaps not. What happens when the navigation is referenced in a view but it doesn't exist?
The ACL package is so specific, it integrates into ZF in a lot of different ways, it needs a Zend_Controller_Plugin, it needs a way of meaningful way of storing and building the Zend_Acl object for querying on an application to application basis.
Sign Up needs a controller, an action and a form which is passed to Zend_View and the form needs to be process. This then needs to be plugged into your ACL object, presumably a database and perhaps various other parts of your site it it requires more specific permissions that fall outside of the use for ACL
Its not impossible to do what you want, but there must be a better way. It almost sounds like your trying to build a CMS with optional, plug-able packages?
The ACL issue I have resolved by having a Library of controllers, helpers, models, forms, etc. A Zend_Controller_Plugin runs and attempts to log in the user, this plugin is run for every app I create, it works well uses an ACL object format which I have used for a while.
For Sign Up I have a RegisterController in my Library, if my application requires registration it has its own RegisterController which extends the RegisterController in the Library. If the application doesn't need registration then it doesn't have its own RegisterController.
I hope that helps, I really think that doing this in the abstracted way suggested isn't worth it and will never be so solid and tight you can truly rely on it because each application is specific.
Generally, in this case you should use modules which are already modularized instead of modularizing the library.
modules/
signup/
models/
plugins/
The default module resource autoloader is configured by default to load plugins:
See: http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html:
If you really need to modularize the library, the customized autoloader will be the best solution (there is an easy to extend abstract autoloader).
You may also consider separate library for each module, added to your include path (not recommended, because it will slow down the app):
modules/
signup/
models/
library/