Multiple regex inside controller instead of mutliple routes with single regex - regex

I run Laravel 5.4.
I have those routes :
<?php
Route::get('/users/{name}', 'UsersController#showByText')->where('name', '[\w]+');
Route::get('/users/{id}', 'UsersController#showById')->where('id', '[\d]+');
?>
And my controllers :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function showByText($name)
{
return DB::table('users')->where('name', 'LIKE', $name)->get();
}
function showById($id)
{
return DB::table('users')->where('id', $id)->get();
}
}
?>
Question :
Could I end up with one single method, to be able to do :
<?php
Route::get('/users/{mixed}', 'UsersController#show');
?>
And my controller would look like :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
function show()
{
if( Request::match('mixed', '[\w]+') )
{
return DB::table('users')->where('name', 'LIKE', $mixed)->get();
}
else if( Request::match('mixed', '[\d]+') )
{
return DB::table('users')->where('id', $mixed)->get();
}
}
}
?>
I know Request::match() does not exists, and knowing that Route::pattern('id', '[\d]+'); will force my method show to be compliant only for digits. I would like to know if there is a shorthand for preg_match() with the Request component.

Related

Completly skip rendering of component

How can I set the component to only show if there are videos?
<?php
namespace App\Http\Livewire;
use App\Models\Video;
use Livewire\Component;
class VideosBrowse extends Component
{
// Computed Property
public function getVideosProperty()
{
return Video::latest()->paginate(10);
}
public function output($errors = null)
{
if (!$this->videos || $this->videos->isEmpty()) {
$this->skipRender();
}
return parent::output($errors);
}
public function render()
{
return view('livewire.videos.browse');
}
}
View:
<div id="videos-browse">
#if ($this->videos && $this->videos->isNotEmpty())
Videos
#endif
</div>
You have to run skipRender() on render
public function render() {
if (!$this->videos || $this->videos->isEmpty()) {
$this->skipRender();
}
return view('livewire.videos.browse');
}

Why listing of items is empty in calling method of the component?

with livewire 2 I have listing of items ($itemDataRows var) and I need for any item show checkbox ($selectedItems var) and
"Select all" button and clicking on this button all items must be selected. I do :
class CrudItems extends Component
{
private $itemDataRows = [];
public $selectedItems = [];
...
public function render()
{
...
$this->itemDataRows = Item
::orderBy('created_at', 'desc')
...
->paginate($backend_per_page);
return view('livewire.admin.items.crud-items', [
'itemDataRows' => $this->itemDataRows,
'item_rows_count' => $this->item_rows_count
])->layout('layouts.admin');
}
}
public function calcSelectedItemsCount()
{
$ret= 0;
foreach( $this->selectedItems as $next_key=>$next_value ) {
if($next_value) {
$ret++;
}
}
return $ret;
}
public function selectAllItems()
{
$this->selectedItems= [];
\Log::info( dump($this->itemDataRows, ' -0 $this->itemDataRows selectAllItems::') );
// INL OG FILE_I SEE THAT ARRAY ABOVE IS EMPTY!!!
foreach( $this->itemDataRows as $nextItemDataRow ) {
$this->selectedItems[$nextItemDataRow->id] = true;
\Log::info( dump($this->selectedItems, ' -$this->selectedItems INSIDE selectAllItems::') );
}
\Log::info( dump($this->selectedItems, ' -$this->selectedItems selectAllItems::') );
}
and in template :
$selectedItems::{{ var_dump($selectedItems) }}<hr>
$itemDataRows::{{ $itemDataRows }}
/* $selectedItems is filled ok when I click on checkboxes , but $itemDataRows shows empty var, though I filled items listing below */
#foreach ($itemDataRows as $item)
<tr>
<td class=" whitespace-nowrap ">
<x-jet-checkbox id="is_reopen" type="checkbox" class="editor_checkbox_field ml-4" title="On saving editor will be opened"
wire:model="selectedItems.{{ $item->id }}"/>
</td>
Is something wrong with definition of $itemDataRows ? Why $itemDataRows is empty in selectAllItems method, but on my template all items are visible ok....
Thanks in advance!
In Livewire you can pass the data via the class variables. And in the mount function you can fill the variable. For Example.
Important Note: The Class Vars must be public!
public $selectedItems = [];
public function mount(): void
{
$this->selectedItems = ['data' => 'Hello World'];
}
public function render()
{
return view('livewire.admin.items.crud-items')->layout('layouts.admin');
}
Update
This must have something to do with the Livewire Lifecyle. Every Livewire component goes through a lifecycle. Lifecycle hooks allow you to run code at any stage of the component's lifecycle or before updating certain properties. In your case, use the mount hook.
You initialise the variable itemDataRows in the render function. A request then calls the method selectAllItems. There you have to initialise itemDataRows again, because the state is no longer there during render or mount.
Solution: create a method getItemDataRows()
private getItemDataRows()
{
$this->itemDataRows => Item::orderBy('created_at', 'desc')
...
->paginate($backend_per_page);
}
then you can call those in the render method and in the selectAllItems method too.
public function selectAllItems()
{
$this->selectedItems= [];
$this->itemDataRows => $this->getItemDataRows();
...
// your code
}

Controller HABTM variables not loading in view

I have two related models in a HasAndBelongsToMany association: Subcategoria has many Paquetes, Paquete has many Subcategorias.
In my subcategory page I need to load the related paquetes, and according to my debug-kit toolbar the variables are being loaded correctly. The problem is the view is showing me an "undefined property" error code. I'm sure I'm missing something, but I can't quite get what's the problem.
My models:
class SubcategoriasTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('subcategorias');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Categorias', [
'foreignKey' => 'categoria_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('Paquetes');
}
}
class PaquetesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('paquetes');
$this->setDisplayField('name');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Subcategorias', [
'foreignKey' => 'paquete_id',
'targetForeignKey' => 'subcategoria_id',
'joinTable' => 'paquetes_subcategorias',
'dependant' => false
]);
}
}
The controller I'm having troubles with:
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\Query;
class SubcategoriasController extends AppController
{
public function content($slug = null)
{
$subcategory = $this->Subcategorias
->findBySlug($slug)
->select(['title', 'id'])
->contain('Paquetes', function (Query $q) {
return $q
->select(['name', 'slug','cost','currency'])
->where(['Paquetes.status' => true]);
});
$this->set(compact('subcategory',$subcategory));
}
}
The corresponging view template (content.ctp):
<?php
$subcategoryName = $subcategory->title;
$title = $subcategoryName;
$this->assign('title', $title);
?>
<h1><?= $title ?></h1>
<?php foreach($subcategory->paquetes as $paquete) : ?>
<!-- Each Paquete's loaded fields. -->
<?php endforeach; ?>
So far I'm getting the following error message:
Notice (8): Undefined property: Cake\ORM\Query::$title [APP/Template\Subcategorias\content.ctp, line 2]
You never call first() or firstOrFail() on the query you built, so in your view $subcategory is still just a Query object, not an Entity.
You probably meant:
$subcategory = $this->Subcategorias
->findBySlug($slug)
->select(['title', 'id'])
->contain('Paquetes', function (Query $q) {
return $q
->select(['name', 'slug','cost','currency'])
->where(['Paquetes.status' => true]);
})
->firstOrFail(); // This is the line that fetches the result and marshals it into an Subcategorias Entity

There is a way to execute a Middleware after controller using withAttribute in the request? (slim 3)

I have to validate some attributes to the files , convert them to png and move them to amazon S3 service , but i need to move the file to the bucket only if the validation in controller is successful, the customer requirement is to use a middle ware to achieve this. is there a way to do it even when is necessary to use the $request-> withAttribute() ?
Yes, indeed. Middleware is just another layer of your callable stack.
Whether it is applied before or after is defined within your code:
<?php
// Add middleware to your app
$app->add(function ($request, $response, $next) {
$response->getBody()->write('BEFORE');
$response = $next($request, $response);
$response->getBody()->write('AFTER');
return $response;
});
$app->get('/', function ($request, $response, $args) {
$response->getBody()->write(' Hello ');
return $response;
});
$app->run();
This would output this HTTP response body:
BEFORE Hello AFTER
So, in your case I'd go for something like this:
<?php
class AfterMiddleware
{
public function __invoke($request, $response, $next)
{
// FIRST process by controller
$response = $next($request, $response);
// You can still access request attributes
$attrVal = $request->getAttribute('foo');
// THEN check validation status and act accordingly
$isValid = true;
if ($isValid) {
// Send to the bucket
}
}
}

ZF2 get template name

Is it possible to retrieve the template name inside of a template (the .phtml thingy)? I can get the ViewModel's template with
echo $this->viewModel()->getCurrent()->getTemplate();
but that doesn't work on partials (obviously). So how can I retrieve the template's name currently being rendered?
You could do this like this:
class Module
{
public function onBootstrap (MvcEvent $e)
{
$eventManager = $e->getApplication ()
->getEventManager ();
$eventManager->attach (
MvcEvent::EVENT_RENDER,
function (MvcEvent $e)
{
$layout = $e->getViewModel ();
$view = reset ($layout->getChildren ());
$view->template1 = $view->getTemplate ();
});
}
and then in the view:
<?php
echo $this->template1;
?>
Simple, but highly effective, solution:
$where_am_i = __FILE__;