I am create class in /module/Application/src/View/Helper/Messages
Code:
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class Messages extends AbstractHelper {
public function __construct() {
}
protected function renderErrorMessages($item) {
echo "!!!";
}
}
Add his in module.config.php
Code:
....
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\UserController::class => InvokableFactory::class,
],
],
'view_helpers' => [
'factories' => [
View\Helper\Messages::class => InvokableFactory::class,
],
'aliases' => [
'messages' => View\Helper\Messages::class
]
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
On page login.phtml i am insert code:
...
<?php echo $this->Messages()->renderErrorMessages(); ?>
...
And get in log error:
[Wed Mar 13 12:18:30.725622 2019] [php7:error] [pid 9697] [client
127.0.0.1:52914] PHP Fatal error: Uncaught Zend\ServiceManager\Exception\ServiceNotFoundException: A plugin by
the name "Messages" was not found in the plugin manager
Zend\View\HelperPluginManager in
/var/www/webuseorg4/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php:142\nStack
trace:\n#0
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(376):
Zend\ServiceManager\AbstractPluginManager->get('Messages', NULL)\n#1
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(394):
Zend\View\Renderer\PhpRenderer->plugin('Messages')\n#2
/var/www/webuseorg4/module/Application/view/layout/login.phtml(32):
Zend\View\Renderer\PhpRenderer->__call('Messages', Array)\n#3
/var/www/webuseorg4/vendor/zendframework/zend-view/src/Renderer/PhpRenderer.php(506):
include('/var/www/webuse...')\n#4
/var/www/webuseorg4/vendor/zendframework/zend-view/src/View.php(207):
Zend\View\Renderer\PhpRenderer->render(NULL)\n#5
/var/www/webuseorg4/vendor/zendframework/zend-mvc/src/View/Http/DefaultRenderingStrateg
in
/var/www/webuseorg4/vendor/zendframework/zend-servicemanager/src/AbstractPluginManager.php
on line 142, referer: http://127.0.0.128/user/login
1
What do I do not so?
Update view-helper configuration with Full qualified class name.
'view_helpers' => [
'factories' => [
Application\View\Helper\Messages::class => InvokableFactory::class,
],
'aliases' => [
'messages' => Application\View\Helper\Messages::class
]
],
And On page login.phtml:
<?php echo $this->messages()->renderErrorMessages($item); ?>
...
Related
I have created a custom endpoint using API Platform:
#[ApiResource(
collectionOperations: [
'get' => [
'normalization_context' => ['groups' => ['product:list']]
],
'post' => [
'denormalization_context' => ['groups' => ['product:list']]
]
],
itemOperations: [
'get_by_user' => [
'method' => 'GET',
'path' => '/products/user/{id}',
'read' => 'false',
'controller' => ProductController::class,
'normalization_context' => ['groups' => ['product:list']],
'openapi_context' => [
'summary' => 'Retrieves the Product collection for a specific user ID',
'description' => 'Retrieves the Product collection for a specific user ID'
]
],
], )]
and the controller method:
public function __invoke(int $id): array
{
$prod = $this->productRepository
->findBy(
['user' => $id],
);
return $prod;
}
And the error when I try to call this endpoint is:
Not Found - api/vendor/api-platform/core/src/EventListener/ReadListener.php
Has anyone any ideas what might be the issue?
I want to add Response code(400,404, 500) for each route in swagger UI using , so that user know in advance what will be the response for these error code.
I am following following link but not sure how can I modify the Response object and can add the response for 400, 404, 401 so on.
https://api-platform.com/docs/core/openapi/#overriding-the-openapi-specification
I am using symfony 5.2 and apiplatfrom 2.6 while writing this question.
When you are using decorators, you are essentially extending existing service code.
First you are getting path and then set your custom operation in swagger context with the given code.
If you notice the code block:
$openApi->getPaths()->addPath('/api/grumpy_pizzas/{id}', $pathItem->withGet(
$operation->withParameters(array_merge(
$operation->getParameters(),
[new Model\Parameter('fields', 'query', 'Fields to remove of the output')]
))
));
The path item take Operation object as an argument which has withResponses method that can be used to modify any responses that your path generates.
Take this as per-cursor and you can check whole source and see what fits your requirements best. Check source : PathItem.php
You can build response according to Response object and add it to Operation and to path.
Tip: Any editor with good linter would make your coding easier as it will be able to show you available methods.
Its late to post but I got the solutions for adding response code in my Swagger Ui, it can help some looking for solution.
private function setErrorResponseDescriptions(OpenApi $openApi): OpenApi
{
$schemas = $openApi->getComponents()->getSchemas();
$schemas['Error'] = [
'type' => 'object',
'properties' => [
'type' => [
'type' => 'string',
'readOnly' => true,
],
'title' => [
'type' => 'string',
'readOnly' => true,
],
'detail' => [
'type' => 'string',
'readOnly' => true,
],
],
];
$schemas['ValidationError'] = [
'type' => 'object',
'properties' => [
'type' => [
'type' => 'string',
'readOnly' => true,
],
'title' => [
'type' => 'string',
'readOnly' => true,
],
'detail' => [
'type' => 'string',
'readOnly' => true,
],
'validations' => [
'type' => 'array',
'readOnly' => true,
'items' => [
'type' => 'object',
'properties' => [
'propertyPath' => [
'type' => 'string',
'readOnly' => true,
],
'message' => [
'type' => 'string',
'readOnly' => true,
],
]
]
]
],
];
foreach ($openApi->getPaths()->getPaths() as $path => $pathItem) {
foreach (['PUT', 'POST', 'PATCH', 'GET'] as $method) {
$item = $pathItem->{'get' . ucfirst($method)}();
if ($item) {
$item->addResponse(
new Model\Response(
description: 'Unauthorized',
content: new \ArrayObject([
'application/problem+json' => [
'schema' => [
'$ref' => '#/components/schemas/Error',
],
],
])
),
'401'
);
if ('GET' !== $method) {
$item->addResponse(
new Model\Response(
description: 'Bad request',
content: new \ArrayObject([
'application/problem+json' => [
'schema' => [
'$ref' => '#/components/schemas/ValidationError',
],
],
])
),
'400'
);
} else {
$item->addResponse(
new Model\Response(
description: 'Not Found',
content: new \ArrayObject([
'application/problem+json' => [
'schema' => [
'$ref' => '#/components/schemas/Error',
],
],
])
),
'404'
);
}
}
}
}
return $openApi;
}
I am using API-Platform with JWTs and have created a decorator to allow Swagger to display a /authentication_token endpoint as described by https://api-platform.com/docs/core/jwt/ and to a lesser degree https://api-platform.com/docs/core/openapi/. In addition to providing an email and password, I also need to provide a third parameter which is a UUID which identifies the account the user belongs to. I was able to do so by including the UUID in the body, but I would rather have it included as a header such as X-Account-UUID.
How can this be modified so that Swagger-UI includes this uuid property as a header and not in the body's JSON?
<?php
// api/src/OpenApi/JwtDecorator.php
declare(strict_types=1);
namespace App\OpenApi;
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\Core\OpenApi\OpenApi;
use ApiPlatform\Core\OpenApi\Model;
final class JwtDecorator implements OpenApiFactoryInterface
{
private $decorated;
public function __construct(OpenApiFactoryInterface $decorated)
{
$this->decorated = $decorated;
}
public function __invoke(array $context = []): OpenApi
{
$openApi = ($this->decorated)($context);
$schemas = $openApi->getComponents()->getSchemas();
/*
echo(json_encode(['class'=>get_class($this), 'methods'=>get_class_methods($this)]));
echo(json_encode(['class'=>get_class($this->decorated), 'methods'=>get_class_methods($this->decorated)]));
echo(json_encode(['class'=>get_class($openApi), 'methods'=>get_class_methods($openApi)]));
echo(json_encode(['class'=>get_class($openApi->getComponents()), 'methods'=>get_class_methods($openApi->getComponents())]));
echo(json_encode(['class'=>get_class($schemas), 'properties'=>$schemas]));
$securitySchemas = $openApi->getComponents()->getSecuritySchemes();
echo(json_encode(['class'=>get_class($securitySchemas), 'properties'=>$securitySchemas]));
$headers = $openApi->getComponents()->getHeaders();
exit(json_encode(['class'=>get_class($headers), 'properties'=>$headers]));
*/
$schemas['Token'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'token' => [
'type' => 'string',
'readOnly' => true,
],
],
]);
$schemas['Credentials'] = new \ArrayObject([
'type' => 'object',
'properties' => [
'email' => [
'type' => 'string',
'example' => 'john.doe#bla.com',
],
'password' => [
'type' => 'string',
'example' => 'secret',
],
'uuid' => [
'type' => 'string',
'in' => 'header',
'example' => '1234abcd-abcd-1234-abcd-123412341234',
],
],
'headers' => [
'uuid' => [
'type' => 'string',
'in' => 'header',
'example' => '1234abcd-abcd-1234-abcd-123412341234',
],
],
]);
$responses = [
'200' => [
'description' => 'Get JWT token',
'content' => [
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Token',
],
],
]
]
];
$content = new \ArrayObject([
'application/json' => [
'schema' => [
'$ref' => '#/components/schemas/Credentials',
],
],
]);
$requestBody = new Model\RequestBody('Generate new JWT Token', $content);
$post = new Model\Operation('postCredentialsItem', [], $responses, 'Get JWT token to login.', '', new Model\ExternalDocumentation, [], $requestBody);
$pathItem = new Model\PathItem('JWT Token', null, null, null, null, $post);
$openApi->getPaths()->addPath('/authentication_token', $pathItem);
return $openApi;
}
}
I just started to build my own web service for alexa skill.
My web service support HTTPS, and the endpoint was hit when I used the alexa simulator for testing, but I got the error 'INVALID_SKILL_RESPONSE'.
This is the simple response from my php code:
return response(
[
'version' => '1.0',
'response' => [
'outputSpeech' => [
'type' => 'PlainText',
'text' => 'Hello world',
]
]
],
200,
[
'Content-Type' => 'application/json',
]
);
What could cause the problem?
I'm not sure where this response function is coming from.
Try something like this:
<?php
$responseArray = [
'version' => '1.0',
'response' => [
'outputSpeech' => [
'type' => 'PlainText',
'text' => 'Hello World'
],
'shouldEndSession' => true
]
];
header( 'Content-Type: application/json' );
echo json_encode( $responseArray );
?>
In my cakephp (3.3) project, How to save multiple tables in a single save. My association is as follows, also I am giving my code here.
Table1 : users
In class UsersTable extends Table
$this->hasOne('ProfileDetails', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
Table2 : profile_details
In class ProfileDetailsTable extends Table
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
What should be the data format to save multiple tables?
In my template I have added the following , for now I have used only few fields
$this->Form->input('User.username',['type' => 'text','class'=>'form-control']) ?>
$this->Form->input('User.password',['type' => 'password','class'=>'form-control']) ?>
$this->Form->input('User.email',['type' => 'text','class'=>'form-control']) ?>
$this->Form->input('ProfileDetail.first_name',['type' => 'text','class'=>'form-control']) ?>
In the controller before saving i have debuged which gives result as follows
debug($this->request->data);
$user = $this->Users->patchEntity($user, $this->request->data);
debug($user);
exit;
$this->Users->save($user)
Result of debug
\src\Controller\UsersController.php (line 39)
[
'User' => [
'username' => 'Tester',
'password' => '43434324',
'email' => 'test#gmail.com',
'role' => 'admin'
],
'ProfileDetail' => [
'first_name' => 'Tester'
]
]
\src\Controller\UsersController.php (line 41)
object(App\Model\Entity\User) {
'User' => [
'username' => 'Tester',
'password' => '43434324',
'email' => 'test#gmail.com',
'role' => 'admin'
],
'ProfileDetail' => [
'first_name' => 'Tester'
],
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'User' => true,
'ProfileDetail' => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [
'email' => [
'_required' => 'This field is required'
]
],
'[invalid]' => [],
'[repository]' => 'Users'
}
Could you please suggest me how to save this data in multiple tables with single save with validation?
Looking at your debug, there is an error saying the email field is required. Maybe that is your problem..
If still not working , try this:
replace User by users
and ProjectDetail by project_details
In your form. And also remove joinType from following:
$this->hasOne('ProfileDetails', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);