How to use a service class in a controller class? - php

I'm trying to integrate this amazing calendar in my project. I get trouble with the service thing. I'm using this kind of structure:
application/
modules/
agenda/
controllers/
CalendarController.php
models/
services/
CalendarService.php
views/
filters/
helpers/
scripts/
calendar/
view-calendar.php
module2/
module3/
I route this in my application.ini this way:
resources.router.routes.agenda.route = /apps/agenda/
resources.router.routes.agenda.defaults.module = agenda
resources.router.routes.agenda.defaults.controller = calendar
resources.router.routes.agenda.defaults.action = view
I pointed my browser on, and I obtained this message:
Message: Invalid controller class ("Agenda_CalendarController")
I fixed it with my Agenda_ suffix and refreshed my browser. Obtained this:
Message: Controller "Agenda_CalendarController" is not an instance of Zend_Controller_Action_Interface
So I instanced it with "extends Zend_Controller_Action". Obtained this: :-)
Fatal error: Class 'Service_Calendar' not found in /home/[PATH TO MY APP FOLDER]/application/modules/agenda/controllers/CalendarController.php on line 30
I can't figure out what's going wrong. Someone have a hint or two for me :-)
Note: I do not know if that means anything, but I use NetBeans and if I press CTRL while hover a class method of service in the controller class; related popup informations are correct.
Solution:
Thanks guys, You gave me the path to go further. My modules bootstraping wasn't set properly. with your hints I found this post from Bob Allen, where he describes the same problem.
When you know what you're searching for... :-)
Thanks, my problem is fixed and I know a bit more!

The naming convention for a controller isn't the same for model/service/view etc., in the sense that you needn't prepend the type in the filename, as you would with a controller. They must follow the basic autoloading name-to-path schema.
So, the declaration for you service would be: Agenda_Service_CalendarService, because that maps to modules/agenda/services/CalendarService.php (note the inflection - from the services folder to the _Service_ singular - that's ZF working and it happens for models, too).
Also make sure you've properly initialized the module (having its own Bootstrap.php file, set up in application.ini).
PS: NetBeans knows where your class is, because it scanned the files and saw the declaration, not the logical inclusion of that class.

Related

Yii 2 gii is generating view files on different directory

I am new to yii framework. On yii version:2.0.1 I have created a module in which I tried to generate a CRUD model using the gii functionality. After putting the model class, controller class and view path when I clicked on generate gii showed all the files has been created successfully. But when I tried to view, below message has been shown to me,
The view file does not exist :
C:\xampp\htdocs\advanced\backend\modules\settings\views\companies\index.php
I found the view files in web directory not in the path I entered. Hence the error.
Here is my inputs to gii,
Model Class:
backend\modules\settings\models\Companies
Search Model Class:
backend\modules\settings\models\CompaniesSearch
Controller Class:
backend\modules\settings\controllers\CompaniesController
View Path:
backend\modules\settings\views\companies
When I click generate I have given below message :
Generating code using template "C:\xampp\htdocs\advanced\vendor\yiisoft\yii2-gii\generators\crud/default"...
generated modules\settings\controllers\CompaniesController.php
generated modules\settings\models\CompaniesSearch.php
generated backend\modules\settings\views\companies\_form.php
generated backend\modules\settings\views\companies\_search.php
generated backend\modules\settings\views\companies\create.php
generated backend\modules\settings\views\companies\index.php
generated backend\modules\settings\views\companies\update.php
generated backend\modules\settings\views\companies\view.php
done!
Does anybody have any idea why is it happening.
Thanks in advance.
I solved similar problem by changing
backend\modules\settings\views\companies
into
#backend/modules/settings/views/companies
Hopefully it helps someone in future
You missed one final folder and alias in View Path. It should be #backend\modules\settings\views\companies.
As you can see in creation log, the generated files are in wrong place (root views folder), that's why the error is thrown.
Have the same problem. Solved entering #backend/modules/settings/views/companies instead backend\modules\settings\views\companies.
See on slashes.
For view path in CRUD generator, enter the absolute or full path. For example
/home/developer/workspace/advanced/backend/views/<your view folder>
put this in the VIEW PATH
#backend/modules/settings/views/companies
and it's done!
I have tried with absolute path i,e
C:/xampp/htdocs/advanced/backend/modules/settings/views/companies
It worked for the absolute path.
I tried this is working by entering:
/Applications/XAMPP/htdocs/advanced/backend/modules/settings/views/companies
I hope you can do it well.
I'm using the advanced theme
Here is my Gii setup in case it is helpful for anyone
module generator
----------------
module class: backend\modules\posts\Module
module id: posts
model generator
---------------
table name: posts
model class: Posts
namespace: backend\modules\posts\models
Enable I18n: checked. category: app
CRUD generator
--------------
Model class: backend\modules\posts\models\Posts
Search model class: backend\modules\posts\models\PostsSearch
Controller class: backend\modules\posts\controllers\PostsController
View path: #backend/modules/posts/views/posts
Enable I18n: checked. category: app
Enable pjax: checked
If you are using some non-default user management (like amnah module),
you need to change Users::className() in the models\Posts.php to
\amnah\yii2\user\models\User::className()
Previously I put a model into the incorrect directory when wanting to achieve an absolute path (address it absolutely as it seems to be more often functional) -
app/backend/modules/settings .
Notice the 'app' in the beginning (the /app is the main directory in Yii starter kit) which I thought would avoid the relative path but must NOT be there nor #mail sign, but on the contrary, it did the opposite - it appended it relatively with its absolute length to the /document root directory, basically duplicated it,
"app/myproject.com/app/backend/modules/settings"
So it unwinds from the model location, the causes of Gii complaints about incorrect paths or put also controllers the incorrect way.
Also strangely enough for views in contrast to models, it had to be put the upper mentioned different way with the # relative reference sign annotation (which was not allowed to be used for controllers or models at the time of writing)
#backend/modules/settings/views/companies, otherwise it appended the directory tree into the document root, again concatenated - the web directory of backend backend/web (backend/modules/settings/views/companies)

How to import resources from a given controller directory structure in Symfony2

I'm working on an application that is growing rapidly which is causing more and more controllers over time and I'm always trying to maintain good practices for do not have as many lines per controller. Right now are almost 40 controllers in Controller directory and it's a bit complicated found one when need to add code or edit or something else so I'm thinking in order them inside subfolders under Controller directory as follow:
src\
AppBundle\
Controller\
Comunes\
CiudadController.php
DuplicadosCedulaController.php
...
RegistroUsuarios\
EmpresaController.php
NaturalController.php
...
RPNI\
CodigoArancelarioController.php
RPNIProductoPaso1Controller.php
...
BuscarEmpresaController.php
DistribuidorController.php
...
But that reorder is causing this error on my application:
FileLoaderLoadException: Cannot import resource
"/var/www/html/project.dev/src/AppBundle/Controller/" from
"/var/www/html/projectdev/app/config/routing.yml". (Class
AppBundle\Controller\EmpresaController does not exist)
Since apparently Symfony is not able to find the controller class when it's not on Controller directory. I've found this topic but is not clear to me what the problem is. I don't know if this is possible or not I read Controller Naming Pattern at Symfony docs but is not so helpful. Any advice around this? A workaround? Suggestions for a better project structure organization?
Note: I made only one bundle because has no sense more than one since the application will not works for separate bundles so following Syfmony Best Practices I come with only one bundle
Edit
This is weird and I don't know how all is working again, I've move all the controllers from Controller to subfolders inside that directory as my example above shows and didn't change nothing at all on routing.yml and Symfony keep getting controllers even if they are in subfolders: amazing!! Ahhh very important just remember CLEAR CACHE command, the most important Symfony command I believe, many of developers issues are cause of this, I forgot complete to clear it and test changes!!
Here is a working example:
routing:
st_mainsiteweb_admin_subsite_create_template:
path: /subsite/create-template
defaults:
_controller: STMainSiteWebBundle:Admin/SubSite:createTemplate
directory structure:
ST\
MainSiteWebBundle\
Controller\
Admin\
SubSiteController -> createTemplateAction
Is this are you looking for?
I do it 2h, now is ok, no routing, no other file.
Just change the namespace of
CiudadController.php
DuplicadosCedulaController.php
....
from AppBundle\Controller to AppBundle\Controller\Comunes
I never tried that so it's just a theoretical answer. If you want to have the controllers like this is perfectly fine, but then you should need to map them in the routing.
Controller\
Comunes\
CiudadController.php
DuplicadosCedulaController.php
Then will be matched in routing yaml:
comunes:
resource: "#yourBundle/Controller/Comunes"
type: annotation
And so on for every different directory. As far as I know they are automatically loaded from Controller/ directory, but if you place them on any other place you need to reference them in the routing.

Easy way to find the controller file with only the URL on Cake PHP

Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}

Where do I save Zend_Form files?

I'm trying to learn Zend Framework! I'm quite interested in it but I can't find a tutorial which says where it's suppoused to be a Zend_Form class stored! Maybe it's something quite straightforward but I can't get it yet...
I've seen tutorials about this:
<?php
class Form_Example extends Zend_Form
{
public function init()
{
// Great code here
}
}
But none of them said where this code goes????? In a file in which folder in the directory tree?? I've read and I understand and I've done a little example with modules, controllers, actions, layouts and I know the importance about name conventions and the folder structure. So where does this form class must go and how can I call it from a view??
Thanks a lot, I know this must be easy for someone who already knows how to work well with Zend Framework =)
The best way to do this is to let ZF do it for you. ZF ships with a command line interface for both windows and *nix.
At the command line you can type zf create form Example, ZF will then create an empty form named Example.php at it's default application level location.
Typically this will be at application/forms/Example.php and the classname will be Application_Form_Example.
If you need to have a form constructed in a module the command would be similar:
zf create form Example -m admin where -m indicates you want the file created in a module and admin is name of the module.
Forms are one of the predefined resources in Zend Framework and as such have a default location. There are several other resources that are predefined and have defaults.
The Module Resource Autoloader
Zend Framework ships with a concrete implementation of
Zend_Loader_Autoloader_Resource that contains resource type mappings
that cover the default recommended directory structure for Zend
Framework MVC applications. This loader,
Zend_Application_Module_Autoloader, comes with the following mappings:
forms/ => Form
models/ => Model
models/DbTable/ => Model_DbTable
models/mappers/ => Model_Mapper
plugins/ => Plugin
services/ => Service views/
helpers => View_Helper
filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and attempted to instantiate the class
"Blog_Form_Entry", it would look in the resource directory's "forms/"
subdirectory for a file named "Entry.php". When using module
bootstraps with Zend_Application, an instance of
Zend_Application_Module_Autoloader will be created by default for each
discrete module, allowing you to autoload module resources.
I normally have all my forms in a forms folder, alongside the models, controllers, and views.
So, my file structure looks like:
application ->
configs
layouts
plugins
controllers
models
views
forms ->
form1.php
form2.php
Using them in your application isn't quite so simple. You must instantiate the form class in your controller, then pass the form to your view. So in your controller you want something like:
$form1 = new Application_Form_Form1($options);
$request = $this->getRequest();
if($request->isPost()) {
if($form1->isValid($post)) {
// form is valid, do form processing here
}
}
$this->view->form1 = $form1;
Then inside of your view file, you place the form:
<html>
<body>
<div id="body">
<?php echo $this->form1; ?>
</div>
</body>
</html>
At the heart of your question are the issues of:
autoloading
how the ZF autoloader works in general, and
how the ZF autoloader is configured by default in a standard ZF app
which are actually three distinct, though clearly-related, issues.
Assuming that you have the default ZF installation in which the appnamespace is set to "Application", then name your form class Application_Form_Example and store it in the file application/forms/Example.php.
Then you can instantiate (in a controller, for example) using:
$form = new Application_Form_Example().
Make sure that you have resources.modules[] = in application/configs/application.ini.
For additional discussion about autoloading, see https://stackoverflow.com/a/10933376/131824

Routing problem with Kohana 3.0

in my current project ive a controller tree like this:
Controller -
------Admin -
------------user.php
------otherClass.php
Where controller and admin are folder and user and otherClass are the classes.
If i want to call any otherClass method the url would be this one:
example.com/otherClass
But when i try to call the users methods like this:
example.com/admin/user
i get this : Class controller_admin does not exist (whats logical), so i tried with the routs in the bootstrap.php and after many failed tries, i gave up and decided to ask you guys :P .
The question is how should i code the route::set to make this work.
Thanks
First thing is that you must set 'index_file' key to FALSE in bootstrap (when Kohana::init() is being called).
Leave the default route as it is ( it's controller/action/id, with controller and action being 'index' by default).
For more informations take a look at kohana 3 routing basics article

Categories