Zend2
Sources Files
Application
modules
default
controllers
ExampleController.php
views
scripts
form
index.phtml
library
square
form
Form_Example.php
Hi All
I am studying Zend Framework: A Beginner's guide chapter 3, i have a form_example class existing in Square/Form/Form_Example.php, it basically has a form in there.
The modules/default/controllers/ExampleControllers.php initialize it. However, i set up
resources.router.routes.example.route = /example
resources.router.routes.example.defaults.module = default
resources.router.routes.example.defaults.controller = form
resources.router.routes.example.defaults.action = form
in the application.ini
when i enter (http://localhost/zend2/public/example), it returns a Page Not Found result back, please help me with this in order to show the Form i create in Form_Example
Thanks, i am really appreciated
I use a separate file for routes, with this format and call it routes.ini
routes.example.route = /example
routes.example.defaults.module = default
routes.example.defaults.controller = form
routes.example.defaults.action = form
In your Bootstrap you then require something like this:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
$router->addConfig($config,'routes');
I find that having a different file for routes makes my main .ini file less complex and it is all easier to understand.
Related
I have a project that contains several subfolders, such as:
client/auth/login = model/view/controller
client/auth/signup = model/view/controller
admin/signin = model/view/controller
I set up my routes like this:
$route['default_controller'] = "admin/signin/signin";
$route['admin/sigin'] = "admin/sigin/signin/index";
$route['admin/(:any)'] = "admin/sigin/signin/";
$route['client/auth'] = "auth/login/login";
These routes are not working, which shows CodeIgniter 404 error page.
$route['default_controller'] = "admin/signin/signin";
$route['admin/signin'] = "admin/signin/signin/index";
$route['admin/(:any)'] = "admin/signin/signin/$1";
$route['client/auth'] = "auth/login/login";
Fixed typo's above.
And I think your file structure is not correct. I use CI2, not sure how modules work in CI3. But the modules 'forgot_password' and 'signin' would be using the same models right? Why having them in separate folders/modules? This way when you make a change to the User model, you'll have to make the change in every User model in all modules (unless you don't need it in that cases, but still I wouldn't risk building my app like that)
- modules
- Admin
- controllers
- user.php // Will have methods like signin(), add(), view(),...
- Client
- controllers
- auth.php // Will have methods like signin(), signout(), ...
- models // This will hold models you don't need in Admin module
// other models should be in the default models folder, so each module will be able to access them.
The routes would look like this:
$route['default_controller'] = "admin/user/signin"; // admin module, user controller, signin method
$route['admin/signin'] = "admin/user/signin";
$route['admin/(:any)'] = "admin/content/$1"; // admin module, content controller, (:any) method (content being an example, I have it in my CMS project)
$route['client/auth'] = "client/auth/login"; // client module, auth controller, login method
I solved it..In config file, I have added the following line of code;
$config['modules_locations'] = array(
APPPATH.'modules/' => '../modules/',
APPPATH.'modules/admin/' => '../modules/admin/',
APPPATH.'modules/client/' => '../modules/client/',
);
It works like a charm..:)
I am noobie at ZF3, We have placed zend based admin panel inside codeigniter based main app. like following way
my_app/zend_admin/
|
|
--- config
--- module
--- public
i can access zend module using www.my_app.com/zend_admin/my_zend_controller/my_zend_action.
I want to access www.my_app.com/my_ci_controller/my_ci_action.
Is there any method zend provide as ci provides base_url() so i can fetch my ci controller??
to get base URL you can use serverUrl view helper (like in codeigniter base_url())
$this->serverUrl(); // return http://web.com OR
$this->serverUrl('/uri'); // return http://web.com/uri
I am not sure about your setup but try that...
There are several ways you can get this job done using ZF micro tools.
There are some similar view helpers in the ZF like CodeIgniter has. You can use them for the purpose in the view script and layout template.
Lets start up using module.config.php of your module. You can set up base_path key under view_manager key as follows
'view_manager' => [
'base_path' => 'http://www.yoursite.com/',
]
Now if you use the following view helper
echo $this->basePath();
// Outputs http://www.yoursite.com/
If you use the following one
echo $this->basePath('css/style.css');
// Outputs http://www.yoursite.com/css/style.css
But if you do not use the above configuration
echo $this->basePath('css/style.css');
// Outputs css/style.css
As #tasmaniski said about $this->serverUrl(); you can use that too in the view script. Good thing for this does not need any configuration like $this->basePath()
What if you need this in the controller action of ZF. The easiest way to do it in the controller action is
public function indexAction()
{
$uri = $this->getRequest()->getUri();
$baseUrl = sprintf('%s://%s/', $uri->getScheme(), $uri->getHost());
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/
}
Otherwise, you can get it the following way but this works same as $this->basePath()
public function indexAction()
{
// This is for zf2
$renderer = $this->getServiceLocator->get('Zend\View\Renderer\RendererInterface');
// This is for zf3
// Assuming $this->serviceManager is an instance of ServiceManager
$renderer = $this->serviceManager->get('Zend\View\Renderer\RendererInterface');
$baseUrl = $renderer->basePath('/uri');
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/uri
}
Moreover, there are two more functions that can be used under different conditions in the controller actions. Those return empty string if rewriting rules used. Those are
$this->getRequest()->getBaseUrl();
$this->getRequest()->getBasePath();
These do not work as you expect I mean as you expect. Must refer to the issue to know why is this!
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
I am trying to set up my zend route using the routes.ini and bootstrap but for some reason it is not able to route as expected. My routes.ini and bootstrap.php are as follows.
routes.ini
[production]
routes.guestbook.route = "/guestbook"
routes.guestbook.defaults.controller = guestbook
routes.guestbook.defaults.action = index
bootstrap.php
protected function _initRoutes()
{
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$router->addConfig(new Zend_Config_Ini(APPLICATION_PATH.'/configs/routes.ini', 'production'), 'routes');
}
After I've read your comment, I can assert that you can delete those statements (config and bootstrap) because what you want to achieve is the normal behavior of the zend framework default router unless you're using modules.
Thanks to FloydThreepwood who remeber me to write this detail.
The easiest way to configure routing is by using the Zend_Application_Resource_Router.
Configuration goes in your application.ini file and that's it, no further code required.
As it appears you're using a static route (no variable path components), try this in your application.ini file
resources.router.routes.guestbook.type = "Zend_Controller_Router_Route_Static"
resources.router.routes.guestbook.route = "guestbook"
resources.router.routes.guestbook.defaults.module = "default"
resources.router.routes.guestbook.defaults.controller = "guestbook"
resources.router.routes.guestbook.defaults.action = "index"
Remove the _initRoutes() method from your Bootstrap class.
Also, this is just an aside but when using other resources such as the front controller in a bootstrap _init* method, you must ensure they've been properly bootstrapped. To do so, retrieve them like this
protected function _initSomething()
{
// make sure resource is bootstrapped
$this->bootstrap('frontController');
// retrieve resource
$front = $this->getResource('frontController');
}
See http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of-operation.bootstrap.dependency-tracking
I would like to keep the old default Zend Router, and just add a router for administration subpages since the controllers are growing in size and I would like to logically separate them a little as well as have cleaner URLs.
The documentation seems to explain how to do other things but not this...
This will work out of the box with the default routes. You just need to add an administration module, and then /administration/users will map to the users controller in the administration module.
I don't know if its possibile to do it with an Underscore and the upcase user, sorry, but without you had to add the following to your bootstrap.php
$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
$route['admin_users'] = new Zend_Controller_Router_Route_Regex(
'administration/users',
array(
'controller' => 'administrationusers',
)
);
$router->addRoute('admin_users_route', $route['admin_users']);
note: in this scenario your controller is:
class AdministrationusersController extends Zend_Controller_Action
{
// stuff
}