how to return Array values in yii2? - php

My view code is:
<?= $form->field($model, 'ReportSelection[]')->dropdownlist(['1' => 'Channel1', '2' => 'channel2','3'=>'channel3','4'=>'channel4'],['multiple'=>'multiple']); ?>
My controller code is:
return $this->redirect(['selectedreport','fromdate'=>$model->FromDate,'todate' => $model->ToDate,'reportselection' =>$model->ReportSelection,'reportoptions' => $model->ReportOption]);
My action function is:
public function actionSelectedreport($reportoptions,$reportselection,$fromdate,$todate)
I need $reportselection as argument here to use in:
foreach($reportselection as $i)
{
$selectlist = $selectlist . 'Channel'.$i.'Value,';
}

All action arguments are scalars by default. If you need accept array, you should explicitly specify this type in method signature:
public function actionSelectedreport($reportoptions, array $reportselection, $fromdate, $todate)

Related

How to create a function in controller and use it in template/view in cakephp 3.xxx

I need to use the function in my view/template page.
My code:
public function getAppNumber($id = null){
$aPPLICATIONINFO = $this->AppEstLto->APPLICATIONINFO->find('all', [
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
foreach($aPPLICATIONINFO as $aPPLICATIONINFO){
$aPPLICATIONINFOx = $aPPLICATIONINFO;
}
return $aPPLICATIONINFOx;
}
You can use set() to use the function variables in your view as given in cookbook:https://book.cakephp.org/3/en/views.html#setting-view-variables
public function get_app_number($id = null){
$applicationInfo = $this->AppEstLto->APPLICATIONINFO->find('all',
[
'fields'=>['application_number'],
'conditions'=>['application_id'=>$id],
'limit' => 200
]);
//Create an array
$applicationArray = new array();
//Store all results in array
foreach($applicationInfo as $application){
$applicationArray = $application;
}
// Pass the array to view
$this->set(compact('applicationArray'));
}
Now, you can use it in your view:
get_app_number.ctp:
<?php
foreach($applicationArray as $application)
{
echo $application['application_number'];
}
?>
You should be doing something like this in your routes.php file inside your Config folder:
Router::connect('/get-app-number', array('controller' => 'yourController', 'action' => 'get_app_number'));
That way you will be able to connect the url that will be used for your view.
Your action corresponds and sends data to your view.
The action is the function that is inside your controller in which you developed for setting the data and variables.
The example of the slug which would be generated:
http://localhost/get-app-number

In CakePhp, how can I retrieve the value of only one column from my database?

I am new to CakePHP but I have been using PHP for a while. I am trying to create a helper that would provide the level of access of a user (ACL).
Here is my ACLHelper.php so far
<?php
namespace App\View\Helper;
use Cake\View\Helper;
use Cake\ORM\TableRegistry;
class ACLHelper extends Helper{
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id]);
return $acl->first();
}
}
I call this function in my view this way
<?= $this->ACL->getACL($user->id, 'is_items', '4') ?>
And this is the output
{ "is_items": "4" }
What I need is the function to return true or false if the value of the field equals or is higher then the value of $level provided to the function. Now if I do this :
<?= $this->ACL->getACL($user->id, 'is_items', '4')->is_item ?>
it will return just the value. My problem is that I do not want to specify the field twice.
Thanks in advance for any help
public function getACL($id, $acl_field, $level){
$members = TableRegistry::get('groups_member');
$group = $members->find()->where(['user_id' => $id]);
$acls = TableRegistry::get('acls');
// Get the first ACL record right here
$acl = $acls->find('all', [ 'fields' => $acl_field ])->where(['group_id' => $group->first()->group_id])->first();
// Compare the requested field against the provided level
return $acl->$acl_field >= $level;
}

i Can't pass three arrays to laravel view

I tried to pass three arrays to a view in laravel
but I got this problem
Undefined variable: demmande (View: C:\wamp\www\project\resources\views\demmande\demmandes.blade.php)
i change the order of the arrays i can't pass the third array
here is my function in the controller
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",$villes,$categorie,$demmande);
}
You can use compact() method.
Try this line to return your data.
return view("demmande.demmandes",compact('villes','categorie','demmande'));
Just replace your code with this,
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = $listvillee;
$demmande = $listdemmande;
$categorie = $listcategorie;
return view("demmande.demmandes",compact('villes','categorie','demmande'));
}
And you can retrieve those variables by
#foreach ($demmande as $data)
{{$data->property}} //your property to define
#endforeach
Hope this will work.
From reading the Laravel Views documentation I think that the view() method expects you to specify the template parameters using one array. You can combine your three arrays into one:
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$data = array(
'villes' => $listvillee,
'demmande' => $listdemmande,
'categorie' => $listcategorie,
);
return view("demmande.demmandes", $data);
}
According to the official documentation you can either pass an array as second parameter, as opposed to the list of all the parameters.
return view("demmande.demmandes", [
'villes' => $villes,
'categorie' => $categorie,
'demande' => $demande
]);
or chain the with method to add more parameters (see the Github page).
return view("demmande.demmandes")
->with('villes', $villes)
->with('categorie', $categorie)
->with('demande', $demande);
The view function will only accept a single array, however, you can nest your arrays within it like this -- and still access them by key from within the view.
return view("demmande.demmandes",['villes'=>$villes, 'categorie'=>$categorie, 'demmande'=>$demmande]);
public function ViewDemmandes(){
$listdemmande=Demmande::all();
$listvillee=Ville::all();
$listcategorie=Categorie::all();
$villes = array('villes' => $listvillee, );
$demmande = array('demmande' => $listdemmande, );
$categorie = array('categorie' => $listcategorie, );
return view("demmande.demmandes",compact('villes','categorie','demmande');
A simple example
View the controller in app/Http/Controllers/SampleController.php
/**
* #return View
*/
public function index()
{
$movieList = [
'Shawshank redemption',
'Forrest Gump',
'The Matrix',
'Pirates of the Carribean',
'Back to the future',
];
return view('welcome', compact('movieList'));
}
and for example, See latest movie views in resources/views/welcome.blade.php
#section('content')
<h1>Latest Movies</h1>
<ul>
#foreach($movieList as $movie)
<li class="list-group-item"><h5>{{ $movie }}</h5></li>
#endforeach
</ul>
#endsection

Laravel: How to get the action names from a controller and their params as a string?

I looking for a function where I give a ControllerName as a param and it returns the functions in that controller. For example:
.
.
.
class ExampleController extends Controller {
public static function firstExample($param1 = ' ', $param2 = 0){
.
.
.
}
public static function secondExample($param3){
.
.
.
}
}
And if I call: $actions = MyController::getActions('ExampleController');
It outputs this structure:
array(
0 => array(
'name' => 'firstExample'
'params' => array(
0 => '$param1',
1 => '$param2',
)
),
1 => array(
'name' => 'secondExample'
'params' => array(
0 => '$param3',
)
)
);
I know how to write functions and controllers. My question is: how to get the function names and their params (and maybe their types if it's possible, too) from a different controller?
So if I understood correctly you want to get the methods of any controller, and their respective parameters.
Step 1 : Getting the methods name
Using the PHP's get_class_methods you can retrieve all the methods of a specific class.
function getActions($controllerName){
$class_methods = get_class_methods(controllerName); //getting them
foreach ($class_methods as $method_name) { //looping them
getParams($controllerName,$method_name);
}
}
Step 2 : Getting the methods parameters
Using PHP's ReflectionMethod class:
function getParams($controllerName,$method_name){
$method = new ReflectionMethod($className, $methodName);
$params = $method->getParameters();
foreach ($params as $param) {
echo $param->getName(); //add to array here
}
}
Now for getting the type, not sure if $param->getType() works, but it's just a matter of trying, anyway this is just the basic stuff, now you can manipulate the function to fit your needs.
Note: Not sure if there's a better way that comes with Laravel, this is a vanilla PHP way of doing it.
Best Regards.

how can I pass an extra parameter to a controller in laravel

I have this:
Route::get('/product/{id}', 'PagesController#display');
And in my PagesController I have this:
public function display($id) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}
How can I pass the variable $premium to the controller method without inserting it in the url? In simple words I don't want this: www.mysite.com/product/false/125 (false is the value of $premium) but this: www.mysite.com/product/125. That's why I have just $id as parameter in the controller method and no also $premium. I want to pass that variable $premium in other way.
I've try some approaches like this one:
Route::get('/product/{id}', 'PagesController#display')->where("premium" => "false");
which didn't work.
You can use a closure route and call the action "manually":
Route::get('/product/{id}', function($id){
return app('PagesController')->callAction('display', [$id, false]);
});
And
public function display($id, $premium) {
return View::make("details", ["id" => $id, "premium" => $premium]);
}

Categories