How in yii2 get controller/action from url.
Attention!!! I ask about Yii2. There are answers that consern only Yii1 at this forum.
Added.
I find smth like Yii::app()->getUrlManager()->parseUrl('http://eewee.djn'));
but in yii2
Added.
I get refferer url to anather conrtoller, where i want to parse it url by yii2 and get controller/action.
That worked for me
$request = new Request(['url' => parse_url(\Yii::$app->request->referrer, PHP_URL_PATH)]);
$url = \Yii::$app->urlManager->parseRequest($request);
list($controller, $actionID) = \Yii::$app->createController($url[0]);
You receive your action in $actionID and controller name in the $controller->id. The Request object needed because such construction:
list($controller, $actionID) =
\Yii::$app->createController(parse_url(\Yii::$app->request->referrer, PHP_URL_PATH));
not works with parameters in url.
Try this!
<?php echo Yii::$app->controller->id; // controller id ?>
<?php echo Yii::$app->controller->action->id; // controller action id ?>
list($controller, $actionID) = Yii::$app->createController($url);
If you need action as Object try this
$action = $controller->createAction($actionID);
Related
Not able to pass array variable form controller to view below code
If, I print in controller it is showing "Undefined variable: data"
public function report(){
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
print_r($data); die();}
Try using compact like this.
public function report(){
$data=$this->Nodals->find()->toArray();
$this->set(compact('data'));
};
Now do dd in your view to check if the data variable sending to the view. I am assuming your view file is report.ctp, so in report.ctp file write this line
<?php
dd($data);
?>
I hope it will help.
$data is not defined in your code if you want to print the query result from
$this->Nodals->find()->toArray(); then you need to store this in $data variable or any other variable
$results=$this->Nodals->find()->toArray();
$this->set('data',$results);
//data will be sent to the template which wrap the $result and in template you can access $reseult by using $data
print_r($results);
die();
or
$data=$this->Nodals->find()->toArray();
$this->set('data',$data);
print_r($data);
die();
How can I pass data from datatable using hyperlink. I want to pass it by id. here is my code.
my controller
my model
index
I want to pass the data here when i click User ID. Thank you.
Student Profile
You can create the hyperlink on foreach loop.
Eg. <?php echo base_url('/users/' . $id ); ?>
After you need to create the route record.
Eg. $routes['/users/(:num)'] = 'login/view/$1';
Lastly, create the method that will receive the ID clicked and do what you want.
public function view( $id ) {
echo $id;
}
I have a problem when passing a variable from Controller to view in CakePHP3.
File FincasController.php: I have created a public Method to make a report and be able to pass to a Template I have also created.
public function worklist()
{
$worklist = TableRegistry::get('Fincas');
$query = $worklist
->find()
->select(['id', 'prov', 'municipio', 'paraje', 'poligono', 'parcela', 'f_ult_poda' , 'f_ult_recog' ]);
foreach ($query as $worklist) {
if ( $worklist->f_ult_poda > $worklist->f_ult_recog )
debug($worklist); //it works fine
}
$this->set('finca', $this->$query); //I have tried also $this->$worklist
}
File Template\Fincas\worklist.ctp :
<?php debug($finca);?>
Thanks a lot
$query it justa a variable, it's not an attribute of your FincasControllerõ. Why are you using$this`?
simply
$this->set('finca', $query);
I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai
I am making a Image link to user profile, but it is not working as it should be
this is my code.with this i want to add contoroller,function and id. How i can do it.
<?php
$pic = $User['User']['url'];
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), array('alt'=>$User['User']['handle'],'title' => $User['User']['handle']),array('escape'=>false) ,array('class'=>'inner_image'));
}else{
echo $this->Html->link($this->Html->image($User['User']['url']),array('alt' => $User['User']['handle']),array('escape'=>false),array('class'=>'inner_image'));
}
?>
This code is making image a link but i can't define a link and it is not accepting the class
.I want to pass this url
$this->Html->link('', array('controller'=>'User','action'=>'view','id'=>$User['User']['id']));
This is how I did it
echo $this->Html->link($this->Html->image($pic, array('class'=>'inner_image')), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false));
What cake version are you using? You don't seem to be following the documentation for Html::link.
HtmlHelper::link(string $title, mixed $url = null, array $options =
array(), string $confirmMessage = false)
alt, escape and class should be indexes in the options array, but you're not defininf the url parameter anywhere.
It should be something like this
if(!$pic){
echo $this->Html->link($this->Html->image('pro.jpg'), $url_array, array('alt'=>$User['User']['handle'],'title' => $User['User']['handle'],'escape'=>false,'class'=>'inner_image'));
} else {
echo $this->Html->link($this->Html->image($User['User']['url']), $url_array, array('alt' => $User['User']['handle'], 'escape'=>false, 'class'=>'inner_image'));
(don't know what url you want to point this at, so replace $url_array to your convenience.