I'm getting a 404 error using Laminas Framework. Can anyone assist? - http-status-code-404

The error is:
A 404 error occurred
Page not found.
The requested URL could not be matched by routing.
No Exception available
I've registered my module and namespace with Composer and then ran Composer dump-autoload. My code is as follows:
module\Album\config\module.config.php
<?php
declare(strict_types=1);
namespace Album;
use Laminas\Router\Http\Segment;
use Laminas\ServiceManager\Factory\InvokableFactory;
return [
'router' => [
'routes' => [
'album' => [
'type' => Segment::class,
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
'view_manager' => [
'template_map' => [
'album/index' => __DIR__ . '/../view/album/album/index.phtml',
],
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
],
];
module\Album\src\Module.php
<?php
declare(strict_types=1);
namespace Album;
class Module
{
public function getConfig() : array
{
return include __DIR__ . '/../config/module.config.php';
}
}
module\Album\src\Controller\AlbumController.php
<?php
declare(strict_types=1);
namespace Album\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class AlbumController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
module\Album\view\album\album\index.phtml
Index page displays here...

As you specified the root as
'route' => '/album[/:action[/:id]]',
The url should look like http://laminas.com/album which will route to the indexAction as you have defined that action as the default, but http://laminas.com/album/index would have the same effect.

Related

Array to string conversion in Route

I'm imploding the Socialite driver from config\auth but getting the error called Array to string conversion
This is the route:
Route::get('redirect/{driver}', 'Auth\LoginController#redirectToProvider')
->name('login.provider')
->where('driver', implode('|', config('auth.socialite.drivers')));
These are the drivers on config.auth.socialite
'socialite' => [
'drivers' => [
'github' => [
'client_id' => env('GITHUB_KEY'),
'client_secret' => env('GITHUB_SECRET'),
'redirect' => env('GITHUB_REDIRECT_URI')
],
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_CALLBACK_URL'),
],
],
],```
You only want the keys listed under drivers, so try something like:
Route::get('redirect/{driver}', 'Auth\LoginController#redirectToProvider')
->name('login.provider')
->where('driver', implode('|', array_keys(config('auth.socialite.drivers'))));

Laravel Multidimensional Array to Collection Object Values

I have an array that I'm wanting to recursively turn into a collection and to use the collection as object values.
I would like to use the collection object in similar ways that eloquent is used rather than using $contact['name'] and being able to use $collection->contacts->each vs foreach $collection->contacts .....)
$collection->contacts->each(function ($contact) {
// ability to use $contact->name (and not have to use $contact['name'])
});
Collection Macro:
Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value)) {
return (object)$value;
}
if (is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});
Example:
public function test_it_can_recursively_convert_array_to_collection()
{
$data = [
[
'name' => 'Michael Scott',
'emails' => [
'mscott#dundermifflin.com',
'michaelscarn#dundermifflin.com',
],
'contacts' => [
[
'name' => 'Dwight Schrute',
'emails' => [
'dschrute#dundermifflin.com',
],
],
[
'name' => 'Jim Halpert',
'emails' => [
'jhalpert#dundermifflin.com',
],
],
],
],
];
$collection = collect($data)->recursive();
$this->assertInstanceOf(Collection::class, $collection);
$collection->each(function ($item) {
$this->assertEquals('Michael Scott', $item->name);
$item->contacts->each(function ($contact) {
$this->assertNotNull($contact->name);
});
});
}
The original collection map works (e.g. $collection->each .... $item->name) but I can't seem to set convert the nested arrays to objects and get the object values.
Error: Call to a member function each() on array
Illuminate\Support\Collection^ {#632
#items: array:1 [
0 => {#13
+"name": "Michael Scott"
+"emails": array:2 [
0 => "mscott#dundermifflin.com"
1 => "michaelscarn#dundermifflin.com"
]
+"contacts": array:2 [
0 => array:2 [
"name" => "Dwight Schrute"
"emails" => array:1 [
0 => "dschrute#dundermifflin.com"
]
]
1 => array:2 [
"name" => "Jim Halpert"
"emails" => array:1 [
0 => "jhalpert#dundermifflin.com"
]
]
]
}
]
}

Issue creating JSON feed in Laravel

I am trying to create a JSON feed using two arrays of data in the format mentioned below.
{
"items":[
{
"category_id":1,
"category_name":"Mens Clothing",
"child":[
{
"product_id":1,
"product_name":"Shirts"
},
{
"product_id":2,
"product_name":"T-Shirts"
}
]
}
]
}
However, I am getting like the following.
{
"items":[
{
"category_id":1,
"category_name":"Mens Clothing",
"child":[
]
}
],
"child":[
{
"product_id":1,
"product_name":"Shirts"
},
{
"product_id":2,
"product_name":"T-Shirts"
}
]
}
I have written the following in Laravel.
$data = [
'items' => [],
];
foreach ($categories as $key => $category) {
$data['items'][$key] = [
'category_id' => $category->id,
'category_name' => $category->category_name,
'child' => [],
];
}
foreach ($products as $key => $product) {
$data['child'][$key] = [
'product_id' => $product->id,
'product_name' => $product->product_name
];
}
Can anyone please help me figure out this issue?
If product has 'category_id' - something like the following can work:
$data = [
'items' => [],
];
foreach ($categories as $key => $category) {
$children = [];
foreach ($products as $key => $product){
if($product->category_id == $category->id) {
$children[] = $product;
}
}
$data['items'][$key] = [
'category_id' => $category->id,
'category_name'=> $category->category_name,
'child' => $children,
];
}

How to use correctly group by in CakePHP3

I want to calculate the number of people in a room based on the model Reservation field Reservation.people (int).
Error:
Reservation not Associated in group
What am I doing wrong?
$hostel = $this->Hostel->get($id, [
'contain' => [
'Room' => [
'conditions' => [
'Room.status' => '0'
],
'sort' => [
'Room.number'
],
'Reservation' => [
'fields' => [
'reservation_room_id' => 'Reservation.room_id',
'sum' => 'SUM(Reservation.people)'
],
'conditions' => [
'Reservation.status' => '0'
],
'group' => [
'Reservation.room_id'
]
]
]
]
]);
You are getting this error because you have not defined association in your model.
Cakephp 2.x
Assuming Reservation has many to one relationship with Group, then you can define your association in below file like following
app/Model Reservation.php
class Reservation extends AppModel {
public $belongsTo = 'Group';
}
Cakephp 3.x
/src/Model/Table/ReservationsTable.php
class ReservationsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Groups');
}
}
Hope this will help!

Checkbox values in prestashop

I'm working with prestashop and try to get value from a form with checkbox using a HelperForm
SO what I had is :
$fields_form[0]['form']= [
'legend'=> [
'title'=> $this->l('Indexation')
] ,
'input'=>[
[
'type'=>'text',
'label'=> $this->l('Base(s) à indexer'),
'name'=>'options',
'size'=>20,
'required'=>true
]
],
'submit'=>[
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
];
and then
$helper = new HelperForm();
[...]
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
'&token='.Tools::getAdminTokenLite('AdminModules'),
),
'back' => array(
'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
// Load current value
$helper->fields_value['options'] = Configuration::get('options');
return $helper->generateForm($fields_form);
and in my getContent I had :
$my_module_name = strval(Tools::getValue('options'));
return $my_module_name;
So until there I had no problem. I write 'test' in the text input and then 'test' is returned but I don't want a text input I want a checkbox input so I changed my form for :
$fields_form[0]['form']= [
'legend'=> [
'title'=> $this->l('Indexation')
] ,
'input'=>[
[
'type'=>'checkbox',
'label'=> $this->l('Base(s) à indexer'),
'name'=>'options',
'required'=>true,
'values'=>[
'query'=>$options,
'id'=>'id',
'name'=>'name'
]
]
],
'submit'=>[
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
];
and $options is :
$options = [
[
'id'=>1,
'name'=>'test'
],
[
'id'=>2,
'name'=>'test2'
]
];
and in my getContent(): return (Tools::getValue('options'));
But with that, nothing is displayed.
Also, if I do return sizeof(Tools::getValue('options)) It give me 1 no matter what I check with the checkbox
First you need to set the name of the field with []
$fields_form[0]['form']= [
'legend'=> [
'title'=> $this->l('Indexation')
] ,
'input'=>[
[
'type'=>'checkbox',
'label'=> $this->l('Base(s) à indexer'),
'name'=>'options[]',
'required'=>true,
'values'=>[
'query'=>$options,
'id'=>'id',
'name'=>'name'
]
]
],
'submit'=>[
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
];
Then, you options should have a value:
$options = [
[
'id'=>1,
'name'=>'test',
'val' => 1
],
[
'id'=>2,
'name'=>'test2',
'val' => 2
]
];
Then you can get the checked values with:
Tools::getValue('options')
Edit:
In 1.6 we have the admin tpl for the helper:
{foreach $input.values.query as $value}
{assign var=id_checkbox value=$input.name|cat:'_'|cat:$value[$input.values.id]}
<div class="checkbox{if isset($input.expand) && strtolower($input.expand.default) == 'show'} hidden{/if}">
{strip}
<label for="{$id_checkbox}">
<input type="checkbox" name="{$id_checkbox}" id="{$id_checkbox}" class="{if isset($input.class)}{$input.class}{/if}"{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if} />
{$value[$input.values.name]}
</label>
{/strip}
</div>
{/foreach}
So, to set the checkbox value to be returned we need to pass the val:
{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}
Also, to be checked or not when loading the page we pass the values to meet the criteria:
{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if}

Resources