i got this method on my routes.php:
Route::resource('maintenance/templates', 'TemplateController', ['names' => createRouteNames('fleet.maintenance.templates')]);
But, i understand, this method break in laravel 5 so, How can i upgrade this method? I understand i need use Route::group( but, i don't know how.
This is one of the tries i did, but, it didn't work:
Route::group(['maintenance/templates' => 'TemplateController'], function(){
Route::resource('template/config', 'ConfigController',[
'only' => ['store', 'update', 'destroy'],
'names' => createRouteNames('fleet.template.config'),
]);
Route::controller('template', 'TemplateController', [
'getTemplates' => 'api.template',
'postService' => 'api.template.service',
]);
});
You don't need the group for the resource function. You can achieve it like this:
Route::resource('maintenance/templates', 'TemplateController', [
'only' => [
'store', 'update', 'destroy'
],
'names' => [
'store' => 'maintenance/templates.store',
'update' => 'maintenance/templates.update',
'destroy' => 'maintenance/templates.destroy',
]
]);
Or you can pass a callable that will return an associative array like the example above:
Route::resource('maintenance/templates', 'TemplateController', [
'only' => [
'store', 'update', 'destroy'
],
'names' => createRouteNames('fleet.maintenance.templates')
]);
Related
tl;dr I have followed the official guide. Everything seems working except for PUT/PATCH verb. The 200 OK code is returned, but the actual model isn't updated. What can be wrong?
I have created a blank Yii 2 project that have created a REST UserController for already existing User model:
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController
{
public $modelClass = 'app\models\User';
}
I have modified the model to have all fields safe:
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_INACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE, self::STATUS_DELETED]],
[['username', 'email'], 'required'],
[['username', 'email'], 'unique'],
['email', 'email'],
[['password_hash', 'password_reset_token', 'verification_token', 'auth_key', 'status,created_at', 'updated_at', 'password'], 'safe'],
];
}
I have configured URL rules to have both pluralized and non-pluralized paths:
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'pluralize' => false,
'except' => ['index'],
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'user',
'patterns' => [
'GET,HEAD,OPTIONS' => 'index',
],
],
],
I have enabled JSON input, if that matters:
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
All the verbs are processed correctly except for PATCH /users/123 / PUT /users/123. When executed in Insomnia, I am getting 200 OK, but the returned record shows no sign of modification:
What can be wrong or what am I missing?
I want to use a different (plural) model name for my route names, because my url is in a different language.
I can Achieve it with Restful Naming Resource Routes
Route::resource('/foo', BarController::class)->parameters([
'foo' => 'bar',
])->names([
'index' => 'bar.index',
'create' => 'bar.create',
'store' => 'bar.store',
'show' => 'bar.show',
'edit' => 'bar.edit',
'update' => 'bar.update',
'destroy' => 'bar.destroy',
]);
But.. I would have to do it for every resource route:
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names([
'index' => 'user.index',
'create' => 'user.create',
'store' => 'user.store',
'show' => 'user.show',
'edit' => 'user.edit',
'update' => 'user.update',
'destroy' => 'user.destroy',
]);
How could I make this DRY?
You can extract that part into a function.
function routeNames($model)
{
return array_map(
fn ($n) => "{$model}.{$n}",
['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']
);
}
Route::resource('/usuarios', UserController::class)->parameters([
'usuarios' => 'user',
])->names(routeNames('user'));
I have created a UserResource that is successfully returning all of my user attributes, including the organization it belongs to. It looks something like this:
Resources/User.php
return [
'type' => 'users',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
'email' => $this->email,
...
'relationships' => [
'organization' => $this->organization,
],
];
In my User model, there is a belongsTo relationship for User->Organization.
Instead of returning the actual organization model, I'd like to return the organization resource.
For example, an organization hasMany locations:
Resources/Organization.php
return [
'type' => 'organizations',
'id' => (string)$this->id,
'attributes' => [
'name' => $this->name,
...
'relationships' => [
'locations' => Location::collection($this->locations),
],
];
I can successfully return the collection of locations that belong to the organization. I have not been able to return a belongsTo relationship.
I've tried:
Resources/User.php
'relationships' => [
'organization' => Organization::collection($this->organization),
],
// or this
'relationships' => [
'organization' => Organization::class($this->organization),
],
// or this
use App\Http\Resources\Organization as OrganizationResource;
...
'relationships' => [
'organization' => OrganizationResource($this->organization),
],
How can I return a single model as a related resource? Thank you for any suggestions!
Have you tried it with the new keyword?
'relationships' => [
'organization' => new OrganizationResource($this->organization),
],
I have a group of route that I apply auth Middleware.
How should I except the tournaments.show ????
I only found examples with $this->middleware syntax, but none with Route::group
Route::group(['middleware' => ['auth']],
function () {
Route::resource('tournaments', 'TournamentController', [
'names' => [
'index' => 'tournaments.index',
'show' => 'tournaments.show',
'create' => 'tournaments.create',
'edit' => 'tournaments.edit', 'store' => 'tournaments.store', 'update' => 'tournaments.update' ],
]);
});
You can except the show route from the resource() as:
Route::group(['middleware' => ['auth']],
function () {
Route::resource('tournaments', 'TournamentController',
[
'names' =>
['index' => 'tournaments.index',
'create' => 'tournaments.create',
'edit' => 'tournaments.edit',
'store' => 'tournaments.store',
'update' => 'tournaments.update'
],
'except' => ['show'],
]
);
});
And then define it outside the group as:
Route::get('tournaments/{id}', 'TournamentController#show')->name('tournaments.show');
I have a problem with payment integration to my laravel project. It is a GOPAY REST API.
It should by default set request headers with Accept, Content-type and Authorization where the token is stored. Problem is that it doesnt set my request headers. I used the same thing in a normal script which included the SDK and it worked correctly. However in my laravel project it just doesnt work. The SDK uses Curl to set headers and i think there is somewhere the problem.
I also didnt find any similar problem, and i definitely didnt google anyone who integrated GoPay into Laravel.
Pay method in my controller
//minimal configuration
$gopay = GoPay\payments([
'goid' => '8583340073',
'clientId' => '1162442589',
'clientSecret' => 'eDxNQ3ru',
'isProductionMode' => false,
'scope' => GoPay\Definition\TokenScope::ALL,
'language' => GoPay\Definition\Language::CZECH],
['logger' => new GoPay\Http\Log\PrintHttpRequest()]);
$response = $gopay->createPayment([
'payer' => [
'default_payment_instrument' => PaymentInstrument::BANK_ACCOUNT,
'allowed_payment_instruments' => [PaymentInstrument::BANK_ACCOUNT],
'default_swift' => BankSwiftCode::FIO_BANKA,
'allowed_swifts' => [BankSwiftCode::FIO_BANKA, BankSwiftCode::MBANK],
'contact' => [
'first_name' => 'Zbynek',
'last_name' => 'Zak',
'email' => 'test#test.cz',
'phone_number' => '+420777456123',
'city' => 'C.Budejovice',
'street' => 'Plana 67',
'postal_code' => '373 01',
'country_code' => 'CZE',
],
],
'target' => ['type' => 'ACCOUNT', 'goid' => '8583340073'],
'currency' => Currency::CZECH_CROWNS,
'order_number' => '001',
'order_description' => 'pojisteni01',
'items' => [
['name' => 'item01', 'amount' => 50],
['name' => 'item02', 'amount' => 100],
],
'recurrence' => [
'recurrence_cycle' => Recurrence::DAILY,
'recurrence_period' => "7",
'recurrence_date_to' => '2016-12-31'
],
'additional_params' => [
array('name' => 'invoicenumber', 'value' => '2015001003')
],
'callback' => [
'return_url' => 'http://www.hume.cz/public',
'notification_url' => 'http://www.hume.cz/public'
]
]);
I think that somehow laravel changes the headers and doesnt allow the SDK to do it. If you know anything please help me. If you need any extra information, please just ask.
Thank you very much!!
There is a package for handling GoPay payments with Laravel. Install, update config with your credentials and start using GoPay facade to createPayment or another function from official SDK.
I have eshop in production with this my own package and everything works fine.
https://github.com/hazestudio/laravel-gopay-sdk