ZF2 Default Routing Configuration - php

I am having trouble with the workflow when creating new modules/controllers to the ZF2 Skeleton Application.
I created a new module test and navigated to mydomain/test. This returns a 404 error until I do the following:
Define my Module in the global config file
Define my route in my module config file
Define my Controller as an invokables in the module config file
Define the path of my view as a view_manager in the module Config file
I am new to ZF2 and trying to better understand the workflow for Application development. This seems like a very cumbersome way to develop, as there is so much configuration needed.
(Rapid application Development??)
Is there a Default means of defining literal routes, controllers, and view rendering in ZF2?

Create your module with zftool, it will add it to your global config.
You will have to create at least one route for each module, take a look at the route in the Application module with this route you're covering allot the comment says:
The following is a route to simplify getting started creating new
controllers and actions without needing to create a new module. Simply
drop new controllers in, and you can access them using the path
/application/:controller/:action
You will have to add controller to the invokables
Use template_path_stack:
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view',
),
)
i use template_path_stack during development and template_map in production

Related

Symfony routing resource and Bundle name

I an trying to include a file into the Symfony 4 routes, and can't figure out what would be the correct way to put in the Bundle name.
My routes.yml:
icatcher_builder:
# loads routes from the given routing file stored in some bundle
resource: '#ICatcher/Builder/Resources/config/routes.yaml'
My bundles.php:
App\ICatcher\Builder\Builder::class => ['dev' => true, 'test' => true],
And I get this error:
An exception has been thrown during the rendering of a template
("Bundle "ICatcher" does not exist or it is not enabled. Maybe you
forgot to add it in the registerBundles() method of your
App\Kernel.php file? in #ICatcher/Builder/Resources/config/routes.yaml
(which is being imported from "[PATH]\config/routes.yaml"). Make sure
the "ICatcher/Builder/Resources/config/routes.yaml" bundle is
correctly registered and loaded in the application kernel class. If
the bundle is registered, make sure the bundle path
"#ICatcher/Builder/Resources/config/routes.yaml" is not empty.").
If I just copy the routes into the main route.yml file instead of including an external resource - all works fine.
Symfony has strict rules of naming Bundle classes, which I found out here in the documentation:
https://symfony.com/doc/current/bundles/best_practices.html
So my Bundle class is now re-named from App\ICatcher\Builder\Builder to App\ICatcher\Builder\ICatcherBuilder and the file is re-named the same way ICatcherBuilder.php and it works.

Yii2 Modules in advanced template

I want to create a module (for example Users) in an advanced template application, that I could use it both in the backend (CRUD functionality) and frontend (login, profile)
Which is the right way of doing it, without having to create one module in backend and another in frontend and maybe a model in the common folder?
I want all files in one folder.
The simplest way for for create a module that you can use both backend and frontend and easily reusable also in other project is create the module in a your vendor dir eg:
vendor\yourvendorname\modulename\Module.php
then create the necessary dir
vendor\yourvendorname\modulename\controllers
vendor\yourvendorname\modulename\models
vendor\yourvendorname\modulename\views
the module name in module section config\main.php
'modules' => [
...
'modulename' => [ // dfenx module for migration via web without console command
'class' => 'vendor\youvendorname\yourmodulename\Module',
],
then refer to the module in you url eg:
yourprojectname/backend/web/index.php/modulename/controller
you can refer to this guide for tutorial

laravel 4 create module and put controller , module and view inside module

I have worked on yii framework before and I had possibility to create module folder and inside this put eg: news module which was included controllers, views and module
I'm new in laravel and trying to create same thing MODULE
i tried the following inside routing
Route::get('/news','/modules/news/controllers/NewsController#index');
file exist but i'm getting
ReflectionException
Class /modules/news/controllers/NewsController does not exist
why ? what i'm doing wrong ?
The Route::get() function is looking for a (auto)loaded Class, not for a file on the disk to load, which is why you're getting these errors.
It's more Laravely (Laravelish?) to include:
Controllers in the /app/controllers/ directory
Views in /app/views/ directory
Models in the /app/models/ directory
And if you are starting out with Laravel, this might be the best way to get started. The autoloader knows where to look for your classes then, and everything gets handled automatically for you.
With the NewsController situated in /app/controllers/ you can do this:
// no need to start the route location with a slash:
Route::get('news', array('uses' => 'NewsController#index'));
You can "package" up functionality using Laravel's Packages, but it would be better to check out the excellent docs and then come back with specific questions..
Put your Laravel controllers in app/controllers, since that directory gets autoloaded, and it is where Laravel expects controllers to be.
Then you can use routes like this (example straight from the docs at http://laravel.com/docs/controllers#basic-controllers)
Route::get('user/{id}', 'UserController#showProfile');

How to add a rule to the Zend URL mapping and compose multiple MVC paths?

This is because I want to develop a web platform with more than one application in the same project.
In any MVC web application we should have this default URL schema:
domain/controller/action/parameters
1: In Zend, what can I do (in which files) to change this schema to add the application name before the controller name?
Expected Result: domain/application/controller/action/parameters
2: How can I implement the consequences of this new URL block in terms that I will separate the MVC for each application, maintaining the shared resources in a separate directory? What changes may I do in Zend autoloader
Expected Result:
/public_html/
/public_html/platform
/public_html/platform/apps
/public_html/platform/apps/base (user interface container)
/public_html/platform/apps/crm
/public_html/platform/apps/crm/model
/public_html/platform/apps/crm/view
/public_html/platform/apps/crm/control
/public_html/platform/apps/crm/public
/public_html/platform/apps/crm/public/css (and etc.)
/public_html/platform/apps/erp
/public_html/platform/apps/erp/model
/public_html/platform/apps/erp/view
/public_html/platform/apps/erp/control
/public_html/platform/apps/erp/public
/public_html/platform/apps/erp/public/js (and etc.)
/public_html/platform/sys
/public_html/platform/sys/core
/public_html/platform/sys/cfg
/public_html/platform/libs/
/public_html/platform/libs/zend
/public_html/platform/libs/template_it
/public_html/platform/libs/custom
i think it is as easy as having actual different ZF2 application, every one in its own folder, and in the same level, a "vendor" folder where you put all the shared structure (coming from zend, third party libraries, etc).
Then inside the vendor folder, i would create another folder for your own shared code, including all your modules that has to be used by more than one of the applications, so your code is a library for yourself.
Since your app is actually in domain/application, and everyone has it own config, it is very straightforward to have domain/application/controller/action/parameters routes: you just create your normal controller/action/parameters routes, since the app actually resides in domain/application/ and the router doesnt have to care about it.
As you noticed, another problem is the autoloader. YOu just need to update the references to the shared modules inside your application.config.php for everyone of your apps
return array(
'modules' => array( //....
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
),
'config_cache_enabled' => false,
'cache_dir' => 'data/cache',
'module_paths' => array(
'./module',
'../vendor',//reference to your library modules
),
),
//...
);
of course, if the modules doesnt reside directly inside vendor/module but something like vendor/libraryname/module, you have to take a look at your autoload system (Composer autoloading or whatever) and add the classes or namespaces to the corresponding maps.

Check what view file ZF2 is trying to load?

I am building my first ZF2 application, and in one of my modules, the views associated with my controller are not loading. Is there a way I can check what view path ZF is trying to execute?
I have checked all of the file paths and module config settings, and they seem correct, and all of my other modules that have the same layout work fine, so I am thinking this is either a filename or namespace issue.
If you use Zend-developer-tools it shows you in the toolbar which layout, template is used.
To your problem - in module you can replace the template from other modules. But it should depend on template path stack.
In module.config.php, you should have something like:
'view_manager' => array(
'template_path_stack' => array(
'application' => __DIR__ . '/../view',
),

Categories