CakePHP 3 - additional Controller namespaces - cakephp-3.0

Is it possible to force CakePHP 3 to read controllers also from other directories, not only default one, e.g. App\Controller\ApiController folder?

You can use routing prefixes for having controller in other namespaces, that would be the built-in solution:
http://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
The other solution is to implement your own ControllerFactoryFilter and based on any arbitrary rules, find the controller in another namespace. This is the original implementation of the factory:
https://github.com/cakephp/cakephp/blob/3.0/src/Routing/Filter/ControllerFactoryFilter.php

Related

In springdocs, can I define multiple OpenAPI definitions based on tags on the operations

I need to define two different OpenApi definitions using springdocs for the same API within a single application: one for internal developers and one for external developers. The external definition would include some of the operations from the internal definition, but not all of them.
I have looked at using GroupedOpenApi to create the two definitions, but this requires that I move the endpoints that should be excluded from the external definition into a separate RestController and move to an excluded package, which would not be included in the definition for the external developers, but would still be included in the internal definition. I would prefer to structure my code based on the API definition and not based on the security access for my endpoints.
This seems like either SecurityScheme or tags could be used to define which operations are included in a given definition using something like GroupedOpenApi with the paths/packages to include. So for example, I could define the definition for my external API with something like the following:
GroupedOpenApi.builder()
.group("externalGroupName")
.securitySchemesToInclude("externalSchemeName") // this doesn't currently exist
.build();
And then any of the operations that are tagged with a SecurityRequirement with that SecurityScheme would get added to this external definition. So for example I could have the following two endpoints defined within the same RestController:
Would be included:
#SecurityRequirement(name = "externalSchemeName")
#GET
#Path("/pets")
public Response getResponse(){
return null;
}
}
Would not be included:
#SecurityRequirement(name = "internalSchemeName")
#GET
#Path("/pets/internal")
public Response getInternalResponse(){
return null;
}
}
With this approach, it would be nice to provide for include/excluded security schemes similar to paths/package inclusions/exclusions.
This seems like it would currently require a contribution to springdocs unless I am misunderstanding my options for how to create multiple definitions. Is there another way to achieve excluding an operation from only one of the definitions I define without completely hiding that operation from all definitions and without restructuring my packages?
Note that I also would prefer not to maintain a list of all the paths that should be excluded from the group in config if possible as this is error prone and doesn't allow for providing a shared config across multiple services. I would prefer an annotation-driven method similar to the way other swagger customization is done so that I define the config once and then update each resource as it is defined or modified based on an annotation to drive the swagger that is generated.
Using you example, you have other filters (based on paths) of GroupedOpenApi, that you haven't used:
GroupedOpenApi.builder()
.group("internalGroupName")
.pathsToMatch("/pets/internal")
.build();
GroupedOpenApi.builder()
.group("externalGroupName")
.pathsToMatch("/pets")
.packagesToExclude("/pets/internal")
.build();

Yii2: differences between init() and bootstrap() and best place to add dynamical URLs on a module

I dont understand exactly what is the difference between use init() and bootstrap() on a class.
My case:
I want to add dynamical urls from my module by using Yii::$app->urlManager->addRules(...) but NOT loading the module in order to improve the performance.
So, I thought if bootstraping the module from the main configuration file like: 'bootstrap' => ['mymodule'], the Module::bootstrap() function will be executed ONLY and exclusively. But actually always runs Module::init() function, and then Module::bootstrap().
On this documentation http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules say:
'Note that you should also list these modules in yii\web\Application::bootstrap() so that they can participate the bootstrapping process.'
But Module::bootstrap() is not executed if the module is listed on yii\web\Application::bootstrap()
I want to set only the dynamic rules with no module loading. How is it possible? What is the best place to set dynamical URLs with no impact to performance?
i decide resolve this issue(adding dynamic rules for module) by watching working extensions.
for example https://github.com/dmstr/yii2-pages-module extension uses bootstrap interface.
don`t forget write in composer.json "type" attribute as "yii2-extension".

Laravel Equivalent of passing variables to functions in codeigniter

I am using CodeIgniter from 2 years and i am trying to move to Laravel and I saw many tutorials of how to use Laravel but i couldn't find any answers to how to pass variables to functions using the URL and without using routes like in CodeIgniter if i called this link
site.com/users/edit/12.
I would be calling the users controller , the edit function and passing a variable of value 12
how can i do the same in Laravel without using routes for every single function or using query strings which will make the URL ugly?
Here is an example.Hope that it will be helpful.......
route.php
Route::controller('users','UsersController');
UsersController.php
class UsersController extends BaseController
{
public function getEdit($value)
{
echo $value;
}
}
url
site.com/users/edit/12
output
12
In Laravel - 3 you may do it using something like this:
Route::controller(Controller::detect());
But in later versions it's not available and you should explicitly declare the routes. Actually it's better to declare routes explicitly and this approach has many benefits too. This is a common problem that happens to developers who migrates from CodeIgniter but later they get motivated and if you use this you'll love this for sure.
You may read this article by Phil Sturgeon who was the man behind the CodeIgniter framework, he discussed about it's down sides of this (automatic routing).
If you are using Laravel - 4 then check the Controllers Manual, specially check out the RESTful and Resourceful controllers section, it may attract you but be familiar with explicit route declaration first.

Angular - building a "public" function (newbie)

I'm After several days learning angularJS through converting my standart JS app to a ng one.
I was wondering about this simple scenario:
I have a global function called fb_connect(),
it can be used from any page (or any controller if you like) to make a facebook-based login.
This function makes a simple http call and receives a JSON object contain data to move on (display a pop up, login, etc...)
I read that I can define a Factory or a Service for my app and use it in any controller, which works fine.
So, I created a fb_connect factory function.
The problem is that now, in every page (every controller), I have to define that fb_connect in the constructor of every controller - for example :
function welcome($scope,fb_connect){});
What is the proper way to do this kind of actions using Angular without having to define these functions each and every time in every controller?
Thanks
Setting up factories and services is all part of the dependency injection system of Angular. Using that system is great when you need to create things that depend on other injected things. It's a big tree of dependencies. It's also nice for creating singletons, such that everywhere in your code end up using the same instance of some object.
It sounds to me like neither of these benefits apply in your case. I'd suggest just not using Angular's DI for it. You have some function defined globally, just call it directly and skip the DI. There's nothing wrong with that.
Of course you say it makes an Ajax call, so doesn't depend on the Angular $http service?
Your two options are:
Declare the function on the $rootScope
Inject it as a service
My advice is to go with making it a service. The whole purpose of services is explained in the Angular.js docs, just like this quote:
Angular services are singletons that carry out specific tasks common to web apps... To use an Angular service, you identify it as a dependency for the dependent (a controller, or another service) that depends on the service.
As you mentioned in your question, you'd prefer to not define the service in every controller you wish to use it in. With $rootScope you'll be injecting that also in every controller. So really it's a question of which you prefer, although to answer your question, the proper way of using a factory or service is to inject it into the controller you wish to use it in.
You can always put it in the $rootScope
myApp.run(function($rootScope, fb_connect){
$rootScope.welcome = function(){
};
});

Associating an Object with other Objects and Properties of those Objects

I am looking for some help with designing some functionality in my application. I already have something similar designed but this problem is a little different.
Background:
In my application we have different Modules. Data in each module can be associated to other modules. Each Module is represented by an Object in our application.
Module 1 can be associated with Module 2 and Module 3. Currently I use a factory to provide the proper DAO for getting and saving this data.
It looks something like this:
class Module1Factory {
public static Module1BridgeDAO createModule1BridgeDAO(int moduleid) {
switch (moduleId)
{
case Module.Module2Id: return new Module1_Module2DAO();
case Module.Module3Id: return new Module1_Module3DAO();
default: return null;
}
}
}
Module1_Module2 and Module1_Module3 implement the same BridgeModule interface. In the database I have a Table for every module (Module1, Module2, Module3). I also have a bridge table for each module (they are many to many) Module1_Module2, Module1_Module3 etc.
The DAO basically handles all code needed to manage the association and retrieve its own instance data for the calling module. Now when we add new modules that associate with Module1 we simply implement the ModuleBridge interface and provide the common functionality.
New Development
We are adding a new module that will have the ability to be associated with other Modules as well as specific properties of that module. The module is basically providing the user the ability to add their custom forms to our other modules. That way they can collect additional information along with what we provide.
I want to start associating my Form module with other modules and their properties. Ie if Module1 has a property Category, I want to associate an instance From data with that property.
There are many Forms. If a users creates an instance of Module2, they may always want to also have certain form(s) attached to that Module2 instance. If they create an instance of Module2 and select Category 1, then I may want additional Form(s) created.
I prototyped something like this:
Form
FormLayout (contains the labels and gui controls)
FormModule (associates a form with all instances of a module)
Form Instance (create an instance of a form to be filled out)
As I thought about it I was thinking about making a new FormModule table/class/dao for each Module and Property that I add. So I might have:
FormModule1
FormModule1Property1
FormModule1Property2
FormModule1Property3
FormModule1Property4
FormModule2
FormModule3
FormModule3Property1
Then as I did previously, I would use a factory to get the proper DAO for dealing with all of these. I would hand it an array of ids representing different modules and properties and it would return all of the DAOs that I need to call getForms(). Which in turn would return all of the forms for that particular bridge.
Some points
This will be for a new module so I dont need to expand on the factory code I provided. I just wanted to show an example of what I have done in the past.
The new module can be associated with: Other Modules (ie globally for any instance of that module data), Other module properties (ie only if the Module instance has a certian value in one of its properties)
I want to make it easy for developers to add associations with other modules and properties easily
Can any one suggest any design patterns or strategy's for achieving this?
If anything is unclear please let me know.
Thank you,
Al
You can use springs Dependency Injection feature. This would help you achieve the flexibility of instantiating the objects using an xml configuration file.
So, my suggestion would be go with the Springs.