Yii2:advanced - several backends - yii2-advanced-app

I want to create several backends.
For example:
Default yii2 advanced project
project/
.. backend/
.. common/
.. console/
.. frontend/
I need:
project/
.. backend1/
.. controllers/
.. modules/
.. backend2 /
.. controllers/
.. modules/
Its really?
The web-site may consist of several subprograms
backend/
b1/
controllers
modules
views
b2/
controlles
modules
views

In Yii2 advanced app you can have as many apps as you want, i.e. backend1, backend2, frontend1, frontendXY, api, etc... you name it.
Here is little guide how to do it: https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/topic-adding-more-apps.md

Related

Catalyst application under Apache2 mod_perl won't render Mason templates

my application root is /home/user/apps/learningcatalyst/CGAddressBook
in that folder I have a /mason folder where mason components are stored
my View is located in /lib/CGAddressBook/View where I have a few files called login_form and addressbook.
Couldn't render component "login_form" - error was "could not find component for initial path '/login_form' (component roots are: '/home/user/apps/learningcatalyst/CGAddressBook/lib/CGAddressBook/View')
is the error I get when trying to reach the site through Apache. My apache conf file is
PerlSwitches -I/home/user/apps/learningcatalyst/CGAddressBook/lib
PerlModule CGAddressBook
<Location /user_catalyst/>
SetHandler modperl
PerlResponseHandler CGAddressBook
</Location>
DocumentRoot /home/user/apps/learningcatalyst/CGAddressBook/root
<Location /user_catalyst/static>
SetHandler default-handler
</Location>
All folders are readable by user, its group, and other. The mason folder, where Mason is set to write its cache, is set to read, write, and execute by user, group, and other. Its owner is user.
I have restarted Apache to no avail, I have changed folder owners (and recursively) of both the /View and /mason.
The application works just fine when running script/cgaddressbook_server.pl -r -p3001 and I go to my site on port 3001.
The path to comp_root is meant to be the path to your template "components". You appear to be pointing it at the same directory as the "View" package. You "might" actually have the components there but that is not really a good practice.
The default location without specifying this should be the "root/comps" folder ( or something like that ) in your Catalyst project structure. One of my own samples is like this:
package SnakierTen::Web::View::HTML;
use Modern::Perl;
use Moose;
use MooseX::NonMoose;
extends 'Catalyst::View::Mason2';
around BUILDARGS => sub {
my ( $orig, $class, $c, #args ) = #_;
$class->config(
comp_root => $c->path_to( 'root' ),
data_dir => $c->path_to( 'data' ),
plugins => [
'TidyObjectFiles',
'HTMLFilters',
],
);
$class->$orig( $c, #args );
};
__PACKAGE__->meta->make_immutable;
no Moose;
1;
Aside from that, direct support of Mod Perl handlers is being deprecated in Catalyst core.
The favored method is to start the application under it's own PSGI compliant server and use a "front end" web server to "proxy" the requests to this application server.
Where you must run the application under a mod_perl environment, it is still recommended to run under a PSGI handler. There are some notes here that are a part of the documentation yet to be built on this. You can use the methods there to guide you in doing this.

Concatenating and minifying RequireJS with Grunt

I have a project written in CoffeeScript that uses AngularJS. My vendor dependancies are installed using Bower and my file structure is like this:
- assets
- js
- app
- model
- *.coffee
- factory
- *.coffee
...
- app.coffee
- config.coffee
- routes.cofeee
- vendor
- angular
- lodash
...
- dist
What I'm trying to do is the following:
I'm trying to work out how I can use RequireJS's r.js to optimise my app files so that I essentially get a concatenated file all ordered nice (so vendor dependancies, my config and routes, and they my app files).
Integrate this into my Grunt file.
I've tried using the r.js optimiser but maybe I've being too silly as all it seems to do is copy my app files (minus the vendor dependancies) into the dist folder; it does, however, manage to optimise the coffee generated js files.
Has anyone got any experience with this?
I figured it out: r.js works by reading your mainConfigFile and any modules you name within your configuration, the important note here is that r.js only looks at the first require/define within your named modules and goes off to seek them; so, for example, I had one named module called app:
require ['config'], (cfg) ->
require ['angular'], (A) ->
A.module cfg.ngApp, []
require ['routes'], () ->
require [
'factory/a-factory',
'service/a-service',
'controller/a-controller'
], () ->
A.bootstrap document, [cfg.ngApp]
The problem here was that r.js never got past the first require statement and thus the concatenation wasn't working. When I changed this to, say (my app.coffee):
require ['config'], (cfg) ->
require ['angular'], (A) ->
A.module cfg.ngApp, []
require ['bootstrap'], (bootstrap) ->
bootstrap()
And my bootstrap.coffee:
define [
'config',
'angular',
'routes',
'factory/a-factory',
'service/a-service',
'controller/a-controller'
], (cfg, A, routes) ->
class Bootstrap
constructor: () ->
routes()
A.bootstrap document, [cfg.ngApp]
This meant that I only needed to define angular and bootstrap in my r.js configuration as includes and then r.js would do the rest, like so:
baseUrl: 'assets/js/app',
mainConfigFile: 'assets/js/app/config.js',
name: 'app',
include: [
'../vendor/requirejs/require',
'bootstrap'
],
out: 'assets/js/dist/app.js'
And now it all works fine! ~~It's a shame that I have to tell r.js to include requirejs though, maybe I've done something silly there?~~
Blimey, I'm such a dingus!
So in my HTML I was loading my concatenated script as:
<script src="assets/js/dist/app.js"></script>
When really it should be loaded like this:
<script src="assets/js/vendor/requirejs/require.js" data-main="assets/js/dist/app"></script>
D'oh!
From r.js doc
https://github.com/jrburke/r.js/blob/master/build/example.build.js#L322
Nested dependencies can be bundled in requireJS > v1.0.3
//Finds require() dependencies inside a require() or define call. By default
//this value is false, because those resources should be considered dynamic/runtime
//calls. However, for some optimization scenarios, it is desirable to
//include them in the build.
//Introduced in 1.0.3. Previous versions incorrectly found the nested calls
//by default.
findNestedDependencies: false,

Padrino + sinatra-assetpack not working

I'm trying to combine Padrino with Sinatra-Assetpack, without success.
This is my Gemfile:
source :rubygems
gem 'rake'
gem 'sinatra-flash', :require => 'sinatra/flash'
# Component requirements
gem 'haml'
# Assets requirements
gem 'sinatra-assetpack', :require => 'sinatra/assetpack'
# Test requirements
# Padrino Stable Gem
gem 'padrino', '0.10.6'
in my app/app.rb file I set:
require 'sinatra/assetpack'
class Coffee < Padrino::Application
register Padrino::Rendering
register Padrino::Mailer
register Padrino::Helpers
register Sinatra::AssetPack
assets {
serve '/js', from: '/app/assets/javascripts'
serve '/css', from: '/app/assets/stylesheets'
css :main, ['/css/main.css']
js :application, ['/js/application.js']
}
enable :sessions
end
my javascript files are in /app/assets/javascripts and css files in /app/assets/stylesheets, but Padrino respond with a 404 for both /css/main.css and /js/application.js
Any ideas?
Thanks
Figured out the issue, in my application anyway, but from the looks of your app.rb code it's probably the same for you;
Assetpack serves files from the directories you specify in your serve calls, relative to your application's root. In padrino, the application root is yourapplication/app, so if you tell assetpack to serve css from /app/assets/stylesheets for instance, its really looking for the files in yourapplication/app/app/assets/stylesheets.
The second part of the problem was that in the AssetPack docs, it shows the code
set :root, File.dirname(__FILE__)
before the register Sinatra::AssetPack line, which I assume is setting the application's root directory properly so that AssetPack will look in the root application directory instead of app. However, even If I modified that call to set to go up one directory from the app.rb file (since it sits in the app dir in Padrino), it didn't seem to have any effect on AssetPack.
In short, modifying the from: paths in the `serve' calls to be relative to your app directory should fix the problem. In your case, they should be:
serve '/js', from: '/assets/javascripts'
serve '/css', from: '/assets/stylesheets'

How to change include directory of Zend Framework

I get the following error messages:
Warning: include_once(Zend\Db.php) [function.include-once]:
failed to open stream: No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: include_once() [function.include]:
Failed opening 'Zend\Db.php' for inclusion (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 83
Warning: require_once(Zend/Exception.php)
[function.require-once]: failed to open stream:
No such file or directory in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
Fatal error: require_once() [function.require]:
Failed opening required 'Zend/Exception.php' (include_path='VPZ/') in
C:\EasyPHP3\www\VPZ\Lib\Zend_1.7.7\Loader.php on line 87
i want to include ZendXXX\Db.php
how to change it
create a directory (say 'lib'), and put your Zend directory in it. so your directory structure looks like this:
- application
- lib
|- Zend
- wwwroot
|- index.php
now you should add lib to your include path. edit your index.php file:
$includePath = array();
$includePath[] = '.';
$includePath[] = './../application';
$includePath[] = './../lib';
$includePath[] = get_include_path();
$includePath = implode(PATH_SEPARATOR,$includePath);
set_include_path($includePath);
now you have your lib in your include path. you can include all Zend components like this:
include 'Zend/Loader.php';
require_once 'Zend/Db.php';
the best way is too include Zend_Loader first and then use it to load classes. do this:
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Db');
you can also register to autoload classes. just add this line to your code after all those before:
Zend_Loader::registerAutoLoad('Zend_Loader',true);
now you do not need to include files to call classes. just instanciate your classes:
$session = new Zend_Session_Namespace('user');
there is no need to include 'Zend/Session/Namespace.php'.
Use set_include_path(). See PHP.net documentation
Example:
set_include_path(get_include_path() . PATH_SEPARATOR . '/path/to/Zend');
I usually store the framework files under a "library" folder:
application
public_html
library
Zend
Common
etc....
and then in my bootstrap file, or front controller, I add that "library" folder to the include path:
set_include_path(get_include_path() . PATH_SEPARATOR . '../library');
See also:
Choosing Your Application's Directory Layout.
Create the Filesystem Layout.
The reason the other suggestions say anything about doing that, is because it's a bad move - in other words, you're doing it wrong.
You can create a subdirectory and name it Zendxxx, but then you have to add that to your include_path, and change it, whenever you put a newly named version up.
I'd hazard a guess, and say that you don't have a good way to test the website (so you want to lock it to a particular version of ZF), and further, that you aren't using revision control, so you want all the previous versions of code in the site-directory to be able to go back to, if you find a problem when you change the live-running code directly on the server.
project without library And including library from one location
project C:\xampp\htdocs\my\application
library C:\xampp\Zend\library
make changes in index.php
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR,
array(realpath(APPLICATION_PATH.'/../../../Zend/library'),get_include_path(),)));

Using forms with the Zend Framework

I'm relatively new to MVC and the Zend Framework. That being said, I feel like I have a hard time figuring out where Forms belong in my directory structure. I have a modular directory structure, so I don't know if there should be a single forms directory, or one within each module directory.
/application
/modules/
/default
/controllers
/views
/admin
/controllers
/views
Once you've decided a directory for forms, do you set that directory in the include path of the bootstrap? Or do you include the form in the controller that it's being used in?
How do you use forms with the Zend Framework?
It's a little late but in the current version of ZF this has been solved:
On the following page http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html
The manual states:
30.3.2. 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:
api/ => Api
forms/ => Form
models/ => Model
DbTable/ => Model_DbTable
plugins/ => Plugin
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.
This does, however, require the use of Zend_Application
add this in application/modules/yourmodule/Bootstrap.php file.
class Yourmodule_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Yourmodule_',
'basePath' => APPLICATION_PATH .'/modules/yourmodule',
'resourceTypes' => array (
'form' => array(
'path' => 'forms',
'namespace' => 'Form',
),
'model' => array(
'path' => 'models',
'namespace' => 'Model',
),
)
));
return $autoloader;
}
}
As of March '09 the ZF thought leaders still seem to be debating the best ways to organize everything. There is a scaffolding-generator as a part of Zend_Tool slated for release in ZF v1.8. It's currently in the incubator, I tried it last week and it works, but there are not many components generated in its current state.
From the examples I've seen it seems that they are best managed separate from the models they interact with (this is from Zend Framework In Action):
/application
/modules/
/default
/controllers
/forms
ContactForm.php
LoginForm.php
RegisterForm.php
SupportForm.php
/models
Comment.php
User.php
Users.php
/views
/admin
/controllers
/views
However, I've also seen structured with the forms below the model directory. Matthew Weier O'Phinney shows how to use them for validation on models themselves:
/application
/modules/
/default
/controllers
/models
Comment.php
User.php
/Form
Comment.php
Login.php
Register.php
/views
/admin
/controllers
/views
To have your files automatically included be sure to name your classes using the underscore model.
For example, when Zend_Loader sees
class RegisterController extends Zend_Controller_Action
It looks in the php include_path for:
Zend/Controller/Action.php
Similarly, assuming the first structure above, if we include the 'default' module in our include_path:
# bootstrap.php
$rootDir = dirname(dirname(__FILE__));
define('ROOT_DIR', $rootDir);
set_include_path(get_include_path()
. PATH_SEPARATOR . ROOT_DIR . '/library/'
. PATH_SEPARATOR . ROOT_DIR . '/application/modules/default/'
);
include 'Zend/Loader.php';
Zend_Loader::registerAutoload();
You name your classes:
Forms_ContactForm
Models_User
Some programmers choose to put most of their files in the library so they don't have to add extra include paths:
/library
/My
/Form
Contact.php
Assuming the library folder is included, the class above would be named:
My_Form_Contact
Best of luck! -Matt
i personally like to keep them in my application folder, since i dont think they belong in the library and having just one folder makes autoloading them easier.
/application
/forms
/modules/
/default
/controllers
/views
/admin
/controllers
/views
/libray/
/Zend
and i just added the form path to the includes_path and i am good to go.
Personally, I found it easiest to put my module directory in the include path and name my form classes in the Zend Loader pattern.
Example directory structure (copying from Matt's answer):
/application
/modules/
/default
/controllers
/forms
Contact.php
Login.php
Register.php
Support.php
/models
Comment.php
User.php
Users.php
/views
/admin
/controllers
/views
Example form class names:
Default_Forms_Contact
Default_Forms_Login
Default_Forms_Register
Default_Forms_Support
I name my models and plugins similarly to keep things simple.
I hope this issue is addressed correctly in later versions of the Zend Framework.
UPDATE:
This structure doesn't work on *nix platforms. Found that out the hard way! The Zend Loader needs the module, forms, and models folders to be capitalized to work in a case sensitive environment.
the zend command tool can create forms for that:
zf create form product sales
where sales is the name of the module, the command tool create a directory form inside module sales and a file Product.php with a class:
class sales_Form_Product extends Zend_Form {
and you have to add a definition of de Zend_Application_Module_Autoloader, to define your module's directory
$selectexamname = new Admin_Form_examresults_Selectexamname();
$this->view->selectexamname = $selectexamname;
your class should have to define according to this format
class Admin_Form_examresults_Selectexamname extends Zend_Form {}
I put all my models in a folder in the library. Notion is my company's name.
/application
/modules/
/default
/controllers
/views
/admin
/controllers
/views
/libray/
/Zend
/Notion
/Form
This makes it easy to include and find files as you already have the library folder included.
Notion_Form_Login
Notion_Db_Manager_Login