Get the id I am passing YII - yii

Im still new to yii and havent been really familar in everything so what i want to do is get the value of the id (xwrg/ixxdfgx.xxx?r=vintages/view&id=2) say my id here is 2 and echo it
Please I hope someone can help

Also, if you use named parameters in your urlManager rules (main/config.php):
'/vintages/<id:\d+>' => 'Vintages/Index', // i.e. http://example.com/vintages/15
Then you can access them like this:
public function actionIndex( $id )
{
echo $id; // Outputs 15
}

Related

Symfony enable to find and delete item

First, this is my code :
/**
*
* #Route("/delete", name="team_delete_v4")
*/
public function deleteTeam(Request $request, BaseEquipe $team, $id)
{
$entityManager = $this->getDoctrine()->getManager();
$repository = $this->getDoctrine()->getRepository(BaseEquipe::class);
$equipe = $repository->find($team->getId());
$entityManager->remove($equipe);
return $this->redirectToRoute('equipe_index_v4');
}
I've a function in my controller where I try to get/find an object by his ID and delete this object. For some reason Symfony is telling me "Unable to guess how to get a Doctrine instance from the request information for parameter "team"." and I can't guess how to resolve it. Please could you help me guys ?
PS : My version is actually 3.4
Thank you very much
In your controller you get 3 parameters, the $request, the $team and an id, but how symfony know witch $team are you sending to your controller. You have to pass the id in the url:
/**
* #Route("/delete/{id}", name="team_delete_v4")
*/
public function deleteTeam(Request $request, BaseEquipe $team)
{
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($team);
return $this->redirectToRoute('equipe_index_v4');
}
Note that the url now is /delete/{id}, when you declare an id in a controller, symfony will find the object with this id. Take a look at the docs.
So in your template, you have to pass the id of the object that you want to delete:
path("team_delete_v4", {id: team.id})
Sorry for my english, hope this help.

Yii2 REST API relational data return

I've set up Yii2 REST API with custom actions and everything is working just fine. However, what I'm trying to do is return some data from the API which would include database relations set by foreign keys. The relations are there and they are actually working correctly. Here's an example query in one of the controllers:
$result = \app\models\Person::find()->joinWith('fKCountry', true)
->where(..some condition..)->one();
Still in the controller, I can, for example, call something like this:
$result->fKCountry->name
and it would display the appropriate name as the relation is working. So far so good, but as soon as I return the result return $result; which is received from the API clients, the fkCountry is gone and I have no way to access the name mentioned above. The only thing that remains is the value of the foreign key that points to the country table.
I can provide more code and information but I think that's enough to describe the issue. How can I encode the information from the joined data in the return so that the API clients have access to it as well?
Set it up like this
public function actionYourAction() {
return new ActiveDataProvider([
'query' => Person::find()->with('fKCountry'), // and the where() part, etc.
]);
}
Make sure that in your Person model the extraFields function includes fKCountry. If you haven't implemented the extraFields function yet, add it:
public function extraFields() {
return ['fKCountry'];
}
and then when you call the url make sure you add the expand param to tell the action you want to include the fkCountry data. So something like:
/yourcontroller/your-action?expand=fKCountry
I managed to solve the above problem.
Using ActiveDataProvider, I have 3 changes in my code to make it work.
This goes to the controller:
Model::find()
->leftJoin('table_to_join', 'table1.id = table_to_join.table1_id')
->select('table1.*, table_to_join.field_name as field_alias');
In the model, I introduced a new property with the same name as the above alias:
public $field_alias;
Still in the model class, I modified the fields() method:
public function fields()
{
$fields = array_merge(parent::fields(), ['field_alias']);
return $fields;
}
This way my API provides me the data from the joined field.
use with for Eager loading
$result = \app\models\Person::find()->with('fKCountry')
->where(..some condition..)->all();
and then add the attribute 'fkCountry' to fields array
public function fields()
{
$fields= parent::fields();
$fields[]='fkCountry';
return $fields;
}
So $result now will return a json array of person, and each person will have attribute fkCountry:{...}

How do I make a construct to have beforeAuth only apply to certain views/functions in Laravel 4

I have a resource in Laravel I have called artists with an ArtistsController. I would like to add filters to some of the pages, but not all. I know I can add a filter to all of the functions/views in the resource controller like so:
public function __construct()
{
$this->beforeFilter('auth', array('except' => array()));
}
How do I add the beforeAuth filter to only a certain view/function? I would like a user to be logged in in order to go the "index" view, but I would like a user to be able to go to the "show" pages without necessarily being logged in:
public function index()
{
$artists = Artist::all();
return View::make('artists.index', compact('artists'))
->with('artists', Artist::all())
->with('artists_new', Artist::artists_new());
}
public function show($id)
{
$artist = Artist::find($id);
return View::make('artists.show', compact('artist'))
->with('fans', Fan::all());
}
Is there a way to do this? Thank you.
Not sure if this helps but you could use the only key instead of the except (if I understand your question correctly).
$this->beforeFilter('auth', array('only' => array('login', 'foo', 'bar')));
Although that would still go in the constructor.

Accessing auth session data (Lithium + MongoDB)

Okay, so hopefully I am asking this question correctly:
I set up my user model & controller, as well as my session model and controller... but I want to render some of the session info onto a page.
for example
If I were to login to a page, it would read "Brian" (or whatever my username is that I used for my login)
I hope I am not asking a repeated question -- I have searched this question pretty extensively and haven't found a solution yet. Thanks a lot!
If your session (set in a config/bootstrap file) is called "default" then just run check ...
$user = Auth::check('default');
Then $user will have an array of the user data in the session, so if you have a first_name field in your database/session you could do:
echo $user["first_name"];
I created a helper to clean this up a little, I called it: extensions/helper/Login.php
<?php
namespace app\extensions\helper;
use lithium\security\Auth;
class Login extends \lithium\template\Helper {
public function user() {
$user = Auth::check('default');
return $user;
}
public function fullName() {
$user = self::user();
return $user["first_name"] . " " . $user["last_name"];
}
}
?>
Then in my Views I used it like ...
<?=$this->login->fullName(); ?>

How to receive a variable in method automatically?

Is it possible to define a method like below to receive $id?
public function action_delete($id){
}
I've defined a route
Route::set('tweet', 'tweet/delete/<id>', array('id' => '\d+'))->defaults(array(
'controller'=>'tweet',
'action'=>'delete'
));
I remember a code snippet something like this few months ago...
Updated: I get the following error message
Missing argument 1 for Controller_Tweet::action_delete()
If your Kohana version < 3.2 then you can use this, however it is highly recommended that you get the id value with $this->request->param('id') -> this is the only way since 3.2 version:
public function action_delete(){
$id = $this->request->param('id');
// Rest of tour code
}
In Kohana 3.2, this doesn't work anymore. You'll have to retrieve the variable through
$id = $this->request->param('id');
See: http://kohanaframework.org/3.2/guide/api/Request#param