Passing array from AuthController to login view in Laravel 5 - authentication

Am having a problem passing an array from Laravel's AuthController to auth.login view. What am trying to do is retrieve data from News model and send it to the view. I know how to use eloquent to retrieve data, passing from the controller to the view is my problem as I cannot see the how/where Laravel is rendering the view.

Add an array as second parameter to the view method when returning it in the controller.
return view('greetings', ['name' => 'Victoria']); // in controller
Then in your view, you should be able to access the variable $name which should be equal to Victoria
var_dump($name); // in view
More in the documentation

I solved it by passing the variable through the Controller on the redirect method.

I am not entirely sure what the objective is here, but you said:
I cannot see the how/where Laravel is rendering the view.
So to answer that:
Laravel's AuthController pulls in a trait AuthenticatesAndRegistersUsers which itself pulls in a few other traits, one being AuthenticatesUsers and another being RegistersUsers.
In AuthenticatesUsers you will find a method like so:
/**
* Show the application login form.
*
* #return \Illuminate\Http\Response
*/
public function showLoginForm()
{
$view = property_exists($this, 'loginView')
? $this->loginView : 'auth.authenticate';
if (view()->exists($view)) {
return view($view);
}
return view('auth.login');
}
There is likewise a similar method in the RegistersUsers trait.
This is where the AuthController returns its views.
If you need to tweak this behavior, or return the view with some data, you could override these methods in your controller, if this is really the best solution to your given situation.

Meanwhile I found a better way to do that.
You can override the showRegistrationForm() method in AuthController.php and pass along the data you want to use in the view. Ex.:
public function showRegistrationForm()
{
$results = Model::all();
return view('auth.register', ['results' => $results]);
}

Related

How to Redirect from one view to another view with data - Yii2

i want to pass 1 variables from a view to another view with post method. use this redirect code but its not working
Yii::$app->getResponse()->redirect('home','id'=>$id)->send();
try this
Yii::$app->getResponse()->redirect(['home','id' => $id])->send();
For the sake of completeness in a controller context you may use:
class MyController extends \yii\web\Controller
{
public function actionIndex()
{
return $this->redirect(['home', 'id' => 123]);
}
}
Where the array parameter of redirect() is equal to yii\helpers\Url::to().

How to pass query string params to routes in Laravel4

I'm writing an api in Laravel 4. I'd like to pass query string parameters to my controllers. Specifically, I want to allow something like this:
api/v1/account?fields=email,acct_type
where the query params are passed along to the routed controller method which has a signature like this:
public function index($cols)
The route in routes.php looks like this:
Route::get('account', 'AccountApiController#index');
I am manually specifying all my routes for clarity and flexibility (rather than using Route::controller or Route::resource) and I am always routing to a controller and method.
I made a (global) helper function that isolates the 'fields' query string element into an array $cols, but calling that function inside every method of every controller isn't DRY. How can I effectively pass the $cols variable to all of my Route::get routes' controller methods? Or, more generally, how can I efficiently pass one or more extra parameters from a query string through a route (or group of routes) to a controller method? I'm thinking about using a filter, but that seems a bit off-label.
You might want to implement this in your BaseController. This is one of the possible solutions:
class BaseController extends Controller {
protected $fields;
public function __construct(){
if (Input::has('fields')) {
$this->fields = Input::get('fields');
}
}
}
After that $fields could be accessed in every route which is BaseController child:
class AccountApiController extends \BaseController {
public function index()
{
dd($this->fields);
}
}

Kohana - Best way to pass an ORM object between controllers?

I have Model_Group that extends ORM.
I have Controller_Group that gets a new ORM:
public function before()
{
global $orm_group;
$orm_group = ORM::factory('Group');
}
...and it has various methods that use it to get different subsets of data, such as...
public function action_get_by_type()
{
global $orm_group;
$type = $this->request->param('type');
$result = $orm_group->where('type', '=', $type)->find_all();
}
Then I have another controller (in a separate module) that I want to use to manipulate the object and call the relevant view. Let's call it Controller_Pages.
$orm_object = // Get the $result from Controller_Group somehow!
$this->template->content = View::factory( 'page1' )
->set('orm_object', $orm_object)
What is the best way to pass the ORM object from Controller_Group to Controller_Pages? Is this a good idea? If not, why not, and what better way is there of doing it?
The reason for separating them out into different controllers is because I want to be able to re-use the methods in Controller_Group from other modules. Each module may want to deal with the object in a different way.
This is the way I would do it, but first I would like to note that you shouldn't use global in this context.
If you want to set your ORM model in the before function, just make a variable in your controller and add it like this.
public function before()
{
$this->orm_group = ORM::factory('type');
}
In your Model your should also add the functions to access data and keep the controllers as small as possible. You ORM model could look something like this.
public class Model_Group extends ORM {
//All your other code
public function get_by_type($type)
{
return $this->where('type', '=', $type)->find_all();
}
}
Than in your controllers you can do something like this.
public function action_index()
{
$type = $this->request->param('type');
$result = $this->orm_group->get_by_type($type);
}
I hope this helps.
I always create an helper class for stuff like this
Class Grouphelper{
public static function getGroupByType($type){
return ORM::factory('Group')->where('type','=',$type)->find_all();
}
}
Now you're been able to get the groups by type where you want:
Grouphelper::getGroupByType($type);

how to implement afterSave method to a specific controller action only?

let's say i have model A..then this model A is being used by many controllers...
now I want to implement the afterSave method, only in one of the controllers that uses
model A . e.g in Controller C it calls the save() function, so I want the afterSave to be called in that function only.how is that ?
protected function afterSave()
{
parent::afterSave();
if($this->isNewRecord)
{
echo "hello";
exit;
}
}
BECAUSE: afterSave() affects all the save() call of all the controllers that uses the Model A
You can try this in you afterSave function :
if (Yii::app()->controller->id!=='yourcontroller')
{
// do what you want
}
If needed, you can also test value of Yii::app()->controller->action->id.
EDIT : or take a look at Jelle de Fries answer.
I don't get why you would need an afterSave() method for this.
In your action you are calling $model->save().
Can't you just do what you have to do after calling this?
like so:
public function actionMyAction(){
$model=new myModel;
$model->attribute = 5;
$model->save();
$model->doLogicAfterSave(); //<-this
$this->render('myView',array(
'model'=>$model,
));
}
Since it's only for 1 controller.

Lithium Framework Architecture - Call One Controller from Another

I'm working on a web app using the Lithium Framework with a MongoDB database.
On one page of the application - I want to display data from multiple object types. I understand the concept of relationships (i.e. belongsTo, hasMany, etc.) between models. But, my questions has to do with Controller relationships.
For example, assume I have two objects named "People" and "Companies". I want to show specific information about Companies on a "people" view. I have done the following:
1) In the "People" model, I've added the following line:
public $belongsTo = array('Companies');
2) In the "PeopleController" file, I've also included a reference to the Companies Model, such as:
use app\models\Companies;
Now, within the PeopleController, I want to call a method in the CompaniesController file.
Do I access this by directly calling the CompaniesController file? Or, do I have to go thru the Company model.
In either case, I'll need help with the syntax. I'm having rouble figuring out the best way this should be called.
Thanks in advance for your help!
You should rethink your structure - you controller method should really grab all the resources you need for that view, it doesn't matter what they are.
So if you have a url '/people/bob' and you want to get the company data for Bob just add that to the view method of your People controller. Something like
People::first(array('conditions' => array('name' => 'Bob'), 'with' => 'Companies'));
You could instantiate a CompaniesController (maybe passing in $this->request to the 'request' option in the process) and then call the method in it. However, a better way to organize it is to move the common functionality from CompaniesController to Companies and call it from both places.
use app\models\Companies does not really make a "reference." It simply indicates that Companies really means app\models\Companies. I think an "alias" is a better way to think of it. See http://php.net/manual/en/language.namespaces.importing.php.
Example:
// in app/models/Companies.php
namespace app\models;
class Companies extends \lithium\data\Model {
public static function doSomething() {
// do something related to companies.
}
}
// in app/controllers/CompaniesController.php
namespace app\controllers;
use app\models\Companies;
class CompaniesController extends \lithium\action\Controller {
public function index() {
$result = Companies::doSomething();
return array('some' => 'data', 'for' => 'the view');
}
}
// in app/controllers/PeopleController.php
namespace app\controllers;
use app\models\Companies;
class PeopleController extends \lithium\action\Controller {
public function index() {
$result = Companies::doSomething();
return array('some' => 'data', 'for' => 'the view');
}
}