Laravel get specific Route parameters - laravel-9

I am from Taiwan, Sorry for my poor English first.
I try to get Laravel specific Route parameters, here are what i have done:
Route :
Route::get('/a', function () {
//
})->name('test.a');
Route::get('/b/{b1}/{b2}', function () {
//
})->name('test.b');
Route::get('/c/{c1}/{c2?}/{c3}', function () {
//
})->name('test.c');
PHP :
function getRouteParameters(?string $routeName)
{
$routeCheck = Illuminate\Support\Facades\Route::getRoutes()->hasNamedRoute($routeName);
if ($routeCheck) {
$routeParameters = [];
$route = Illuminate\Support\Facades\Route::getRoutes()->getByName($routeName);
$uri = $route->uri();
$uriArray = explode('/', $uri);
$uriParameters = array_filter($uriArray, function ($value) {
return str_contains($value, '{');
});
foreach ($uriParameters as $uriParameter) {
$uriParameterName = str_replace(['{', '?', '}'], '', $uriParameter);
$uriParameterStatus = str_contains($uriParameter, '?') ? 'optional' : 'required';
$routeParameters[$uriParameterName] = $uriParameterStatus;
}
return $routeParameters;
}
return false;
}
$a = getRouteParameters('test.a');
$b = getRouteParameters('test.b');
$c = getRouteParameters('test.c');
$d = getRouteParameters('test.d');
dump($a, $b, $c, $d);
Result :
// $a
[]
// $b
array:2 [▼
"b1" => "required"
"b2" => "required"
]
// $c
array:3 [▼
"c1" => "required"
"c2" => "optional"
"c3" => "required"
]
// $d
false
It's there a better way to do this ( without write the function by myself )?

Related

Node.js Sequelize: multiple condition on 'where'

How do i query a table with multiple conditions? This give me no error but, only run the first condition!
exports.findAllLimit = (req, res) => {
const titulo = req.query.titulo ;
var condition = titulo ? { titulo : { [Op.iLike]: `%${titulo }%` } } : null;
var condition2 = {stock: { [Op.ne]: 0}};
Produtos.findAll({
where: condition , condition2,
include: [Categorias],
order: [
['id', 'ASC']
],
limit: 9
})
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Ocorreu um erro a retirar os dados do backend!."
});
});
};
You create here an object with property condition2 and it's value.
You need to merge these 2 conditions, and assign them on where.
so you can use:
where: Object.assign({}, condition , condition2),
OR:
where: {...condition, ...condition2}
you can do like this for multiple condition .
const titulo = req.query.titulo ;
var condition = titulo
? {
titulo: {
[Op.iLike]: `%${titulo}%`,
},
}
: null;
var condition2 = {
stock: {
[Op.ne]: 0,
},
};
let where = [];
where.push(condition);
where.push(condition2);
Produtos.findAll({
where,
});

Errors while debugging code using HTML::TreeBuilder::XPath

I am getting some errors when I try to debug the following code.
Note that it fetches the data from approx 6,000 fields from the http://europa.eu/youth/volunteering/evs-organisation#open
After parsing each page, check for the existence of the next › link at the bottom.
View-source is a browser based command. It tells the browser to output the response in plain text rather than render it based on its actual content type, HTML in this case. You should not need to include view-source in your URL.
Here we have a script that extracts the data out of each block and cleans it up a little. The browse function is generic. It takes an input reference which contains the URL and XPaths of the parent and children in order to construct the output ref. It is just an approach: it does not yet navigate across each page,
In a rough script I tested, I fetched the total results using //span[#class="ey_badge"] then the max page using
my $page_max = $results / 21;
$page_max = int( $page_max ) == $page_max ? $page_max-- : int( $page_max ) ;
See the errors
martin#linux-3645:~/dev/perl> perl eu.pl
syntax error at eu.pl line 81, near "our "
Global symbol "$iterator_organizations" requires explicit package name at eu.pl line 81.
Can't use global #_ in "my" at eu.pl line 84, near "= #_"
Missing right curly or square bracket at eu.pl line 197, at end of line
Execution of eu.pl aborted due to compilation errors.
martin#linux-3645:~/dev/perl> ^C
martin#linux-3645:~/dev/perl>
It fetches the data from approx 6,000 fields from http://europa.eu/youth/volunteering/evs-organisation#open
See the code
use strict;
use warnings FATAL => qw#all#;
use LWP::UserAgent;
use HTML::TreeBuilder::XPath;
use Data::Dumper;
my $handler_relurl = sub { q#https://europa.eu# . $_[0] };
my $handler_trim = sub { $_[0] =~ s#^\s*(.+?)\s*$#$1#r };
my $handler_val = sub { $_[0] =~ s#^[^:]+:\s*##r };
my $handler_split = sub { [ split $_[0], $_[1] ] };
my $handler_split_colon = sub { $handler_split->( qr#; #, $_[0] ) };
my $handler_split_comma = sub { $handler_split->( qr#, #, $_[0] ) };
my $conf = {
url => q#https://europa.eu/youth/volunteering/evs-organisation_en#,
parent => q#//div[#class="vp ey_block block-is-flex"]#,
children => {
internal_url => [ q#//a/#href#, [ $handler_relurl ] ],
external_url => [ q#//i[#class="fa fa-external-link fa-lg"]/parent::p//a/#href#, [ $handler_trim ] ],
title => [ q#//h4# ],
topics => [ q#//div[#class="org_cord"]#, [ $handler_val, $handler_split_colon ] ],
location => [ q#//i[#class="fa fa-location-arrow fa-lg"]/parent::p#, [ $handler_trim ] ],
hand => [ q#//i[#class="fa fa-hand-o-right fa-lg"]/parent::p#, [ $handler_trim, $handler_split_comma ] ],
pic_number => [ q#//p[contains(.,'PIC no')]#, [ $handler_val ] ],
}
};
print Dumper browse( $conf );
sub browse {
my $conf = shift;
my $ref = [ ];
my $lwp_useragent = LWP::UserAgent->new( agent => q#IE 6#, timeout => 10 );
my $response = $lwp_useragent->get( $conf->{url} );
die $response->status_line unless $response->is_success;
my $content = $response->decoded_content;
my $html_treebuilder_xpath = HTML::TreeBuilder::XPath->new_from_content( $content );
my #nodes = $html_treebuilder_xpath->findnodes( $conf->{parent} );
for my $node ( #nodes ) {
push #$ref, { };
while ( my ( $key, $val ) = each %{ $conf->{children} } ) {
my $xpath = $val->[0];
my $handlers = $val->[1] // [ ];
$val = ( $node->findvalues( qq#.$xpath# ) )[0] // next;
$val = $_->( $val ) for #$handlers;
$ref->[-1]->{$key} = $val;
}
}
return $ref;
}
{
'internal_url' => 'https://europa.eu/youth/volunteering/organisation/948417016_en',
'external_url' => 'http://www.apd.ge',
'location' => 'Tbilisi, Georgia',
'title' => '"Academy for Peace and Development" Union',
'topics' => [
'Access for disadvantaged',
'Youth (Participation, Youth Work, Youth Policy)',
'Intercultural/intergenerational education and (lifelong)learning'
],
'pic_number' => '948417016',
'hand' => [
'Receiving',
'Sending'
]
}
our $iterator_organizations = sub {
my ( $browser, $parent ) = #_;
my $url = q#https://europa.eu/youth/volunteering/evs-organisation_en#;
my $nodes = $browser->nodes( url => $url );
my $iterator = sub {
return shift #$nodes;
};
return ( $iterator, 1 );
our $iterator_organizations_b = sub {
my ( $browser, $parent ) = #_;
my $url = q#https://europa.eu/youth/volunteering/evs-organisation_en#;
my $uri = URI->new( $url );
my $xpath = q#//div[#class="vp ey_block block-is-flex"]#;
my $nodes = [ ];
my $page = 0;
my $results = $parent->{results};
my $page_max = $results / 21;
$page_max = int($page_max) == $page_max ? $page_max-- : int($page_max);
my $iterator_uri = sub {
$uri->query_form( page => $page++ );
return $page > 2 ? undef : $uri ; # $page_max;
};
my $iterator_node = sub {
unless ( #$nodes ) {
my $uri = $iterator_uri->( ) // return undef;
my $options = $page == 1 ? { tree => $parent->{_node} } : { url => $uri->as_string };
$nodes = $browser->nodes( %$options, xpath => $xpath );
}
return shift #$nodes;
};
return ( $iterator_node, 0 );
};
our $iterator_organization = sub {
my ( $browser, $parent ) = #_;
my $url = $parent->{internal_url};
my $nodes = $browser->nodes( url => $url );
my $iterator = sub {
return shift #$nodes;
};
return ( $iterator, 1 );
};
sub organizations {
my ( $self, $options ) = ( shift, { #_ } );
my $map = [
$Massweb::Browser::Europa::iterator_organizations,
results => q#.//span[#class="ey_badge"]#,
organizations => [
$Massweb::Browser::Europa::iterator_organizations_b,
internal_url => [ q#.//a/#href#, $Massweb::Browser::Europa::handler_url ],
external_url => [ q#.//i[#class="fa fa-external-link fa-lg"]/parent::p//a/#href#, $Massweb::Browser::handler_trim ],
title => q#.//h4#,
topics => [ q#.//div[#class="org_cord"]#, $Massweb::Browser::handler_val, $Massweb::Browser::handler_list_colon ],
location => [ q#.//i[#class="fa fa-location-arrow fa-lg"]/parent::p#, $Massweb::Browser::handler_trim ],
hand => [ q#.//i[#class="fa fa-hand-o-right fa-lg"]/parent::p#, $Massweb::Browser::handler_trim, $Massweb::Browser::handler_list_comma ],
pic_number => [ q#.//p[contains(.,'PIC no')]#, $Massweb::Browser::handler_val ],
recruiting => [ q#boolean(.//i[#class="fa fa-user-times fa-lg"])#, $Massweb::Browser::handler_bool_rev ],
_ => \&organization,
],
];
my $organizations = $self->browse( map => $map );
return $organizations;
}
sub organization {
my ( $self, $options ) = ( shift, { #_ } );
my $map = [
sub { $Massweb::Browser::Europa::iterator_organization->( $_[0], $options ) },
#title => q#.//h1#,
description => q#.//div[#class="ey_vp_detail_page"]/p#,
];
my $organization = $self->browse( map => $map );
return $organization;
}
The problem appears to be the block/anonymous hash starting 'internal_url'. I can't imagine what you intend there but it is a syntax error and would have no effect if you fixed it
Why are you declaring so many subroutine references like our $iterator_organizations = sub { ... } instead of using standard subroutines? It is a very strange approach

Yii2-How to access a variable from model to a controller?

I am working on yii2. I have came across a point in which I have to send an email to a person when a meter is installed and it's images are uploaded to the server. Fro this I have already configured the swift mailer.
There is a model named Installations which have a function which saves all the installation data.
public static function saveAll($inputs){
$coutner = 0;
$arr_status = [];
foreach ($inputs as $input) {
$s = new Installations;
foreach ((array)$input as $key => $value) {
if($key != 'image_names') {
if ($s->hasAttribute($key)) {
$s->$key = $value;
}
}
}
$user = Yii::$app->user;
if (isset($input->auth_key) && Users::find()->where(['auth_key' => $input->auth_key])->exists()) {
$user = Users::find()->where(['auth_key' => $input->auth_key])->one();
}
$s->created_by = $user->id;
if (Installations::find()->where(['ref_no' => $input->ref_no])->exists()) {
$arr_status[] = ['install_id' => $input->install_id, 'status' => 2, 'messages' => "Ref # Already exists"];
continue;
}
$s->sync_date = date('Y-m-d H:i:s  ');
if($s->save()){
if ($s->istallation_status == 'Installed') {
Meters::change_status_byinstall($s->meter_msn, Meters::$status_titles[4]);
}
else if ($s->istallation_status != 'Installed' && $s->comm_status =='Failed')
{
Meters::change_status_byinstall($s->meter_msn, Meters::$status_titles[5]);
}
$arr_status[] = ['install_id' => $input->install_id, 'status' => 1];
$coutner++;
if (isset($input->doc_images_name)) {
foreach ($input->doc_images_name as $img) {
$image = new InstallationImages;
$image->image_name = $img->image_name;
$image->installation_id = $s->id;
$image->save();
}
}
if (isset($input->site_images_name)) {
foreach ($input->site_images_name as $img2) {
$image2 = new InstallationImagesSite;
$image2->image_name = $img2->image_name;
$image2->installation_id = $s->id;
$image2->save();
}
}
}else{
$arr_status[] = ['install_id' => $input->install_id, 'status' => 0, 'messages' => $s->errors];
}
$status = $s->istallation_status;
$msn = $s->meter_msn;
$com = $s->comm_status;
// want to pass these variables to the controller function
}
return ['status' => 'OK', 'details' => $arr_status, 'records_saved' => $coutner];
}
Now There Is a Controller name InstallationController. This controller contains all the APIs for my mobile application. Below are two main functions in it
public function actionAddnew()
{
$fp = fopen('debugeeeeeee.txt', 'w+');
fwrite($fp, file_get_contents('php://input'));
fclose($fp);
$inputs = json_decode(file_get_contents('php://input'));
return Installations::saveAll($inputs);
}
public function actionSavephoto()
{
try {
$count = 0;
foreach ($_FILES as $f) {
$dd = pathinfo($f['name']);
if (!isset($dd['extension']) || !in_array($dd['extension'], array('jpg', 'png', 'gif'))) {
return ['status' => 'ERROR', 'uploaded_files' => $count, 'message' => 'Invalid File'];
break;
}
if (move_uploaded_file($f['tmp_name'], Installations::UPLOAD_FOLDER . $f['name'])) {
$count++;
return ['status' => 'OK', 'uploaded_files' => $count];
break;
} else {
return ['status' => 'ERROR', 'uploaded_files' => $count];
break;
}
}
} catch (Exception $x) {
return ['status' => 'ERROR', 'message' => $x->getMessage()];
}
}
The mobile application will call the Addnew() api and after that it will call the savephoto. Now I want to pass $msn,$status and $com values from the Model to the controller function Savephoto.
For this I have tried to use session variables but still I am unable to get by desired result(s).
I have also checked the question Yii, how to pass variables to model from controller?
but it didn't worked for me.
How can I achieve it?
Any help would be highly appreciated.
The only way to get those values out of saveAll() is to return them. Presently, they are defined on an object in $s that is overwritten each loop. The best way to do that seems to be creating an array outside of your foreach ($inputs... loop and appending each created Installations object.
Return that at the end, and pass it (or just the relevant element from it) into actionSavephoto() as a parameter. Then, those values will be accessible of properties of that passed object. This handling will occur in the code that is not pictured which calls actionAddNew() and then actionSavephoto()

Customize error message in yii2 REST API

I want to customize this following error message
Error 404: Not Found
{
"name": "Not Found",
"message": "Object not found: 6",
"code": 0,
"status": 404,
"type": "yii\\web\\NotFoundHttpException"
}
to:
Error 404: Not Found
{
"name": "Not Found",
"message": "Country not found for: 6",
"code": 404,
"status": Error
}
Where is need to write this customization code?
try this (e.g. config/web.php):
return [
// ...
'components' => [
// ...
'response' => [
// ...
'on beforeSend' => function (yii\base\Event $event) {
$response = $event->sender;
if (404 === $response->statusCode && is_array($response->data)) {
$response->data['code'] = $response->data['status'];
$response->data['status'] = 'Error';
unset($response->data['type']);
}
},
],
You need to override errorHandler which extends yii\web\ErrorHandler, just convertExceptionToArray method
/**
* Converts an exception into an array.
* #param \Exception|\Error $exception the exception being converted
* #return array the array representation of the exception.
*/
protected function convertExceptionToArray($exception)
{
if (!YII_DEBUG && !$exception instanceof UserException && !$exception instanceof HttpException) {
$exception = new HttpException(500, Yii::t('yii', 'An internal server error occurred.'));
}
$array = [
'name' => ($exception instanceof Exception || $exception instanceof ErrorException) ? $exception->getName() : 'Exception',
'message' => $exception->getMessage(),
'code' => $exception->getCode(),
];
if ($exception instanceof HttpException) {
$array['status'] = $exception->statusCode;
}
if (YII_DEBUG) {
$array['type'] = get_class($exception);
if (!$exception instanceof UserException) {
$array['file'] = $exception->getFile();
$array['line'] = $exception->getLine();
$array['stack-trace'] = explode("\n", $exception->getTraceAsString());
if ($exception instanceof \yii\db\Exception) {
$array['error-info'] = $exception->errorInfo;
}
}
}
if (($prev = $exception->getPrevious()) !== null) {
$array['previous'] = $this->convertExceptionToArray($prev);
}
return $array;
}
The code above is from Yii, you just need to tweak it a little.
Then add it to config : ( usually web.php)
'errorHandler' => [
'class' => 'your\namespace\YourErrHandler',
],
Im not sure what I do is right or wrong but it works.
Just create a new custom php file called ErrorMsg.php
<?php
use Yii;
use yii\web\HttpException;
class ErrorMsg extends \Exception
{
public static function customErrorMsg($error_code,$message = null, $code = 0, \Exception $previous = null,$extra_content=NULL)
{
$httpException = new HttpException($error_code,$message,$code,$previous);
Yii::$app->response->statusCode = $error_code;
$custom_err = array(
'name'=> $httpException->getName(),
'message' => $message,
'code' => $code,
'extraContent' => $content,
'status' => $error_code
);
return $custom_err;
}
and call the functions wherever you want. Example
return ErrorMsg::customErrorMsg(400,"Message Here",11,NULL,"Extra Content Here");

Customizing Joomla PDF output

In Joomla 1.5 constructor of JDocumentPDF class has an array parameter to setup some parameter of generated PDF.
function __construct($options = array()) {
parent::__construct($options);
if (isset($options['margin-header'])) {
$this->_margin_header = $options['margin-header'];
}
if (isset($options['margin-footer'])) {
$this->_margin_footer = $options['margin-footer'];
}
if (isset($options['margin-top'])) {
$this->_margin_top = $options['margin-top'];
}
...
}
_createDocument() function of JFactory class instantiates JDocumentPDF object, but doesn't pass any options that useful for PDF generation:
function &_createDocument() {
...
$attributes = array (
'charset' => 'utf-8',
'lineend' => 'unix',
'tab' => ' ',
'language' => $lang->getTag(),
'direction' => $lang->isRTL() ? 'rtl' : 'ltr'
);
$doc =& JDocument::getInstance($type, $attributes);
return $doc;
}
So I don't understand how it works and where can I set this options (margin-header, margin-footer etc)?
To set and get any properties of JDocumentPDF
you can call set and get function on object. For example
$obj = JFactory::getDocument();
$marginHeader = $obj->get('_margin_header');
$obj->set('_margin_header', $value);