Register all sub namespace in Phalcon - phalcon

I am new to Phalcon. The document states we can register namespace as follow
$loader->registerNamespaces(
[
'Example\Base' => 'vendor/example/base',
'Example\Adapter' => 'vendor/example/adapter',
'Example' => 'vendor/example',
]);
It needs to register every sub namespaces under Example.
But I saw here Izo states that with suitable folder structure and namespace prefix, we only need to specific the root namespace like this
$loader->registerNamespaces([
'Example' => 'vendor/example'
])
Izo gave an example:
say you have a folder directory /var/www/myapp/Plugins/,
if you have file : /var/www/myapp/Plugins/Auth/Users.php, this file has to be
with namespace MYAPP\Plugins\Auth and class name has to be Users
I cannot find any document talking about it, can anyone helps?

Related

Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false

I have 2 projects ones call Api which is a library for my DbContext and other call AdminPanel which is an Asp.NetCore project for my website.
in The Api Project my Dbcontext inherit IdentityDbContext<AppUser> and the
Appuser class is in the same project in model folder which inherited from IdentityUser class.
Now in my AdminPanel in Startup.cs i use
services.AddIdentity<ApplicationUser, IdentityRole>(config =>
{
config.User.RequireUniqueEmail = true;
config.Password.RequiredLength = 8;
})
.AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();
services.AddScoped<SignInManager<ApplicationUser>, AuditableSignInManager<ApplicationUser>>();
which ApplicationUser is a class in AdminPanel now when i run AdminPanel project i'll get
An error occurred during the compilation of a resource required to
process this request. Please review the following specific error
details and modify your source code appropriately.
Generated Code
One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the
'PreserveCompilationContext' property is not set to false.
The type or namespace name 'ApplicationUser' could not be found (are you missing a using directive or an assembly reference?)
I don't know what should i do.
beforehand Sorry about my English. Thanks for your help.

Is there a way to add two resource folders to aspnet core localization?

I'm currently developing the SDK for one project and as a requirement I need to add two resources locations. One will be provided with the SDK lib and another to be provided by the consumer app.
Currently, according to docs, this is how to add localization:
services.AddLocalization(options => options.ResourcesPath = "Resources");
I'm calling this method from my BaseStartup class that will be inherited by the consumer app's Startup class. So I need to be able to setup the location of the SDK's resources folder and the consumer app's one as well.
Maybe something like:
services.AddLocalization(options =>
{
options.ResourcesPath = "SDKResources";
options.FromAssembly = sdkResourcesAssembly;
});
services.AddLocalization(options =>
{
options.ResourcesPath = "AppResources";
options.FromAssembly = appResourcesAssembly;
});
Is this possible? If so, how? If not, is there a workaround?
Checking online and even the source code (https://github.com/aspnet/Localization) wasn't of much help. The only thing I can think of is using IStringLocalizerFactory which accepts an assembly and the name of the file. Would it work? For instance, adding services.AddLocalization() and then just creating a wrapper class that would provide the consumer app with the strings using the factories created using IStringLocalizerFactory?
Thanks!
I found out about two ways it can be done, first by adding resources from different assemblies:
I created a base startup class to handle all source assemblies containing my resource classes and then I load them.
serviceCollection.AddLocalization();
var resourceTypes = typeof(BaseResource<>).Assembly.GetDerivedGenericTypes(typeof(BaseResource<>));
if (typeFromResourceAssembly != null)
resourceTypes.AddRange(typeFromResourceAssembly.Assembly.GetDerivedGenericTypes(typeof(BaseResource<>)));
foreach (var resourceType in resourceTypes)
{
serviceCollection.AddScoped(resourceType, resourceType);
}
return serviceCollection;
Second, by adding different resource folders:
services.Configure<ClassLibraryLocalizationOptions>(
options => options.ResourcePaths = new Dictionary<string, string>
{
{ "ResourceClass", "ResourcesFolder" },
{ "Localization.CustomResourceClass", "Folder1/Folder2" }
}
);

What is causing the error that swagger is already in the route collection for Web API 2?

I installed Swagger in my ASP.Net MVC Core project and it is documenting my API beautifully.
My co-worker asked me to install it in a full framework 4.6.1 project so I've done the following.
In Package Console Manager run:
Install-Package Swashbuckle
To get your Test Web API controller working:
1) Comment this out in the WebApi.config:
// config.SuppressDefaultHostAuthentication();
// config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
Now this URL:
http://localhost:33515/api/Test
brings back XML:
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>value1</string>
<string>value2</string>
</ArrayOfstring>
In Global.asax Register() I call:
SwaggerConfig.Register();
In AppStart.Swagger.Config Register() I put:
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1.0", "HRSA CHAFS");
c.IncludeXmlComments(GetXmlCommentsPath());
})
.EnableSwaggerUi(c =>
{});
}
private static string GetXmlCommentsPath()
{
var path = String.Format(#"{0}bin\Services.XML", AppDomain.CurrentDomain.BaseDirectory);
return path;
}
}
Now I get this error:
"A route named 'swagger_docsswagger/docs/{apiVersion}' is already in the route collection. Route names must be unique."
I've been stuck on this for hours.
How do you get rid of this?
This can happen when you re-name your .NET assembly. A DLL with the previous assembly name will be present in your bin folder. This causes the swagger error.
Delete your bin folder and re-build your solution.
This resolves the swagger error.
Swagger config uses pre-application start hook, so you don't need to call SwaggerConfig.Register() explicitly. Otherwise Register method is called twice.
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
in my case i added another project as refrence and that other project has swagger too.
i remove that refrence and move needed code to new project.
I solved the problem by deleting the SwaggerConfig.cs file from the App_Start folder as I had already created it manually.
Take a look at this link, here also has more useful information:
A route named 'DefaultApi' is already in the route collection error
In my experience the error occurs when you add reference to another project and that project is a service and it occurs on the SwaggerConfig of the referenced project. Removing project reference usually solve the problem, if you need to share classes I suggest you to create a specific project as Class Library and add its reference to both your services

yii-user extension include(WebUser.php) No such file or directory

i'm trying to install the extension of yii-user following this official tutorial
http://www.yiiframework.com/extension/yii-user/#hh2
But i'm having some problems, specially when i'm adding this
user'=>array(
// enable cookie-based authentication
'class' => 'WebUser',
'allowAutoLogin'=>true,
'loginUrl' => array('/user/login'),
to the configuration main. When i add this code, i have this message error
include(WebUser.php) [function.include]: failed to open stream: No such file or directory
Any clue? I need to do something before?
Thanks in advance
I searched a little bit and i found the solution. But it wasn't in the documentation.
So, we should create the WebUser.php in protected/components like this :
<?php
// this file must be stored in:
// protected/components/WebUser.php
class WebUser extends CWebUser {
// Store model to not repeat query.
private $UserLogin;
// Return first name.
// access it by Yii::app()->user->first_name
function getFirst_Name(){
$user = $this->loadUserLogin(Yii::app()->user->user_id);
return $user->first_name;
}
// This is a function that checks the field 'role'
// in the User model to be equal to 1, that means it's admin
// access it by Yii::app()->user->isAdmin()
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->user_id);
return intval($user->user_role_id) == 1;
}
// Load user model.
protected function loadUserLogin($id=null)
{
if($this->UserLogin===null)
{
if($id!==null)
$this->UserLogin=UserLogin::model()->findByPk($id);
}
return $this->UserLogin;
}
}?>
and should work.
Did you follow the instructions at http://www.yiiframework.com/extension/yii-user/#hh2?
You probably forgot to specify import paths to the user module in config.php
'import'=>array(
...
'application.modules.user.models.*',
'application.modules.user.components.*',
),
I had the same problem and found that it's the permission problem. Apache user (www-data in my case) couldn't access protected/modules/users/* files.

Doctrine2: testing repository classes with YAML config

I have YAML config for my symfony2 project using Doctrine2. I'm not understanding how exactly to adapt the cookbook entry to a YAML setup.
My doctrine mapping is at /path/to/my/bundle/Resources/config/doctrine/IpRange.orm.yml
When running PHPUnit, I get the error:
Doctrine\ORM\Mapping\MappingException: No mapping file found named 'Yitznewton.FreermsBundle.Entity.IpRange.orm.yml' for class 'Yitznewton\FreermsBundle\Entity\IpRange'.
Sounds like I need to configure the test rig to use the symfony file naming conventions, but I don't know how to do that.
Using symfony-standard.git checked out to v2.0.7
Here's my test:
<?php
namespace Yitznewton\FreermsBundle\Tests\Utility;
use Doctrine\Tests\OrmTestCase;
use Doctrine\ORM\Mapping\Driver\DriverChain;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use Yitznewton\FreermsBundle\Entity\IpRange;
use Yitznewton\FreermsBundle\Entity\IpRangeRepository;
class IpRangeRepositoryTest extends OrmTestCase
{
private $_em;
protected function setup()
{
// FIXME: make this path relative
$metadataDriver = new YamlDriver('/var/www/symfony_2/src/Yitznewton/FreermsBundle/Resources/config/doctrine');
$metadataDriver->setFileExtension('.orm.yml');
$this->_em = $this->_getTestEntityManager();
$this->_em->getConfiguration()
->setMetadataDriverImpl($metadataDriver);
$this->_em->getConfiguration()->setEntityNamespaces(array(
'FreermsBundle' => 'Yitznewton\\FreermsBundle\\Entity'));
}
protected function getRepository()
{
return $this->_em->getRepository('FreermsBundle:IpRange');
}
public function testFindIntersecting_RangeWithin_ReturnsIpRange()
{
$ipRange = new IpRange();
$ipRange->setStartIp('192.150.1.1');
$ipRange->setEndIp('192.160.1.1');
$this->assertEquals(1, count($this->getRepository()
->findIntersecting($ipRange)),
'some message');
}
Looking again at the symfony docs, it seems that integration testing with a live test database is preferred to unit testing for repository classes. That is, there is no support for stubbing EntityManagers.