I'm attempting to override the routes provided by ZfcUser using akrabat's method, however it doesn't seem to have any effect. I'm starting by rebasing the routes from /user to /users but will want to add other routes later on (should be simple if I can get this working).
My DI configuration:
return [
'di' => [
'instance' => [
'Zend\Mvc\Router\RouteStack' => [
'parameters' => [
'routes' => [
'zfcuser' => [
'options' => [
'route' => '/users'
]
]
]
]
]
]
]
];
Am I making an obvious mistake or has the configuration structure for this changed since this blogpost?
You'd overwrite configuration from within your own modules.
'router' => array(
'routes' => array(
'zfcuser' => array(
//...
)
)
)
Though if i'm correct the full DI Path should still work... It's important, too, that your module loads AFTER the zfcUser-Module. So within your application.config.php be sure that your modules Namespace is listed after zfcUser. Since the arrays simply get overwritten depending on load time ^^
Related
I am using Laravel Sanctum to provide a personal access token to my clients.
I am using these guards in auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'client' => [
'driver' => 'token',
'provider' => 'clients',
'hash' => false
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
],
I give my clients tokens using the following code:
$client = Client::find($id);
$token = $client->createToken('CLIENT_TOKEN');
but whenever I try to access the user with $request->user() I get the wrong user. while the token is completely different from what the retrieved user is.
I am using laravel-swoole package as my webserver.
I need one route to fetch page content out of my CMS by a slug. I thought I could easy create a configuration like the following but it seems that it only works with multiple parts in the route. My plan was to create a custom route, but if I don't get a match with the system routes I don't think I will get one with my custom route :)
So is there a way to do that?
Does not work
'router' => [
'routes' => [
'cms' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/:slug',
'defaults' => [
'controller' => \Cms\Controller\PageController::class,
'action' => 'index'
]
]
]
]
];
Works
'router' => [
'routes' => [
'cms' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/test/:slug', // <-- Here is the change
'defaults' => [
'controller' => \Cms\Controller\PageController::class,
'action' => 'index'
]
]
]
]
];
OK, forgett everything... I already had a segment route with the same format, but with some constraints. Therefore I got a 404 page. So everything works fine...
In an Apigility driven Zend Framework 2 application wenn a database adapter is created (over the Apigility Admin UI), its settings by default get saved in /config/autoload/global.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(),
...
),
),
...
);
and in /config/autoload/local.php
return array(
'db' => array(
'adapters' => array(
...
'DbAdapter_FooBar' => array(
'charset' => 'UTF-8',
'database' => 'asdf',
'driver' => 'PDO_Mysql',
'hostname' => 'asdf',
'username' => 'asdf',
'password' => 'asdf',
'port' => '1234',
'driver_options' => array(
1002 => 'SET NAMES \'UTF8\'',
),
),
...
),
),
...
);
In the application I'm working on the config files structure differs from the ZF2 standard, e.g. there are separate config files for the database settings: /config/autoload/mydb.global.php and /config/autoload/mydb.local.php.
(How) Can Apigility be configured in the way, that the database adapters settings get stored in custom config files? How/where to set these files?
Configuration for various components can be provided in multiple places, but all configuration is merged recursively into one large configuration file by the Zend\Stdlib\ArrayUtils::merge() method. Because configurations are merged recursively, the order that they are added to the merged configuration array is really important to avoid unexpected overwriting.
Configurations are merged in the following order:
The array returned by the module’s getConfig() method
config/autoload/*.global.php — global autoload files
config/autoload/*.local.php — local autoload files
The methods described by the Feature interface — get*Config()
I started writing this as I was struggling to get ZF2 Asset Manager to work properly however in mid swing I managed to sort out the problem and felt the question still needs to be asked in order to help others.
If you are looking for an Asset Manager for ZF2, try out: https://github.com/RWOverdijk/AssetManager
The problems I faced was essentially due to a lack of documentation and the solution was really simple.
For asset manager to work, you essentially need to generate a cache of your files that your app can locate. Once the files have been located and are reachable, your app will speed up tremendously. The first problem I faced was the module.config.php file. The documentation is not clear in how to set this up correctly.
My original file looked like this:
'asset_manager' => [
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
Which essentially tells my application to look in the module/assets directory for any assets. This worked out the box but was VEERY slow...
I read up about caching and my modification of my file was as follows:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
This did nothing... so after searching code in gitHub to see how others had implemented Asset Manager I discovered you can set the options of where to put the cache... so my new config became this:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => [
'dir' => getcwd() . 'public',
]
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
Still my app was not working although the assets where being generated in a folder called: trunkpublic in the root of my app. After much frustration I realised that I need to update:
'dir' => getcwd() . 'public', to 'dir' => getcwd() . '/public',
Adding a slash put the folder in the public directory, which in retrospect is obvious since the app needs access to the cached files.
My final configuration is this:
'asset_manager' => [
'caching' => [
'default' => [
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => [
'dir' => getcwd() . '/public',
//'dir' => getcwd() . '/data/cache/assets',
]
],
],
'resolver_configs' => [
'paths' => [
__DIR__ . '/../assets',
]
],
],
You will notice I have this snippet commented out:
//'dir' => getcwd() . '/data/cache/assets',
The above is super fast and is used with the Filepath option and is great for development mode, it essentially generates a hash of the file in the /data.cache/assets directory however for a production environment I prefer to have actual files saved to the assets cache found in: public/assets as this allows the app to grab the files directly without having to invoke php.
NOTE:
There was another issue I faced and this is due to misleading documentation
If you look at this line:
'cache' => 'Filesystem', // Apc, FilePath, FileSystem etc.
You may be tempted to add your files using CamelCase... however some may reauire CamelCase and others may require Normalcase... check the code for this...
works for me!!
'asset_manager' => array(
'caching' => array (
'default' => array(
'cache' => 'FilePath', // Apc, FilePath, FileSystem etc.
'options' => array(
'dir' => './public',
),
),
),
'resolver_configs' => array(
'paths' => array(
'MyModule' => __DIR__ . '/../public',
),
),
),
now i try fix flter JSMin and JSSqueeze
tank you!
I'm going through the zend 2 getting started tutorial and I hit a wall. I am at the point in the tutorial where my action controller loads a view via the indexAction():
public function indexAction() {
return new ViewModel(array(
//$albums inside index.phtml will contain data from this method
'albums' => $this->getAlbumTable()->fetchAll()
));
}
But when loading the page I see this error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
At this point I realized I don't know what the hell is happening. I don't even know where to begin troubleshooting this error. Before I scan all of the files for typos I'd really like to understand how this error can occur.
here is my modul.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' =>
'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'ablum' => __DIR__ . '/../view',
),
),
);
The error "Unable to render template "album/album/index" means that you have to add the index.phtml file under the /album/album directory under the 'Album' module's view directory. The index.phtml view template file is used for rendering the view for the index action of the AlbumController controller of the Album module. Because this file seems to be missing, the view template resolver couldn't find it.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory of the module.
For beginner, I would recommend to read the Using Zend Framework 2 book. With this e-Book, you can save your time and efforts learning ZF2.
Also, the path is case sensitive i.e. it should be all be in lowercase album/index/index both folders' name and index.phtml else phpRenderer will not be able to trace the view file and render it.
You can also check if you have the following in your module.config.php
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],