cakephp is amazing but this auth stuff i haven't gotten all the pieces, my question is simple. I want to pass the current logged in users array data so i can use it in a view. I use animal names as my testing names
class UsersController extends AppController {
public function cat() {
$this->set('cat',$this->Auth->login($this->request->data) );
//I also tried $this->Auth->login()
}
}
now my view
//cat.ctp
<div>
<?php echo print_r($cat)?> //returns 11
</div>
Alternatively you can access user info via session helper in view file:
<?php print_r($this->Session->read('Auth.User')); ?>
In your controller, $this->Auth->user() will return you an array with current user data.
You can retrieve authentified user's data from the controller using
$this->Auth->user();
exp: $this->Auth->user('username');
Related
I have a Controller witch contains more than 150 functions. almost all of the functions inside the controller are returning views.
each view should change if the user is logged in.
I almost handled this in the view side by using parent blades and etc. But in the controller side for almost every function i should check if the user is logged in and return some specific data about the user along with the view.
for example:
$user=[];
if(Auth::check())
{
$user=Auth::user;
$reputation=$user['reputation'];
$messages=$user->messages();
$notifications=$user->notification();
}
return view('pages.profile')->with(['user'=>$user , 'messages'=>$messages , 'notifications'=>$notifications]);
I thought that this is possible using middlewares but i could not figure it out.
what are the alternatives here?
for more information i am using laravel 5.
One solution could be using your custom function instead of view(). Sth like:
//in your controller action
public function someAction() {
return $this->view('pages.profile');
}
protected function view($template, $data = array()) {
return view($template)->with($data)->with([data that needs to be passed to all views]);
}
I am using CodeIgniter and I have two different controllers - login.php and site.php
In login.php
function credentials(){
$user = $this->input->post('username');
// there is some code here
}
In site.php, I have function members_area() I need to pass the variable $user from login.php to site.php. How do I do this ?
Thanks
If you are talking about user logins here.. In your Login controller you verify user credentials. If so then you need to set a session variable
$this->sessions->set_userdata('user_id',$user_id);
redirect('site/members_area');
Then in your Site controller you retrieve the data for that user from DB.
$current_user = $this->sessions->userdata('user_id');
And you get your required data from DB.
just take a redirect to site/members_area/$user
and you'll get $user parameter in the function members_area($user)
Turns out that a controller is used to control what is sent to a view. If the design is right you're not really supposed to send data between two controllers.
I sent inserted the value of username into a session.
So, In login.php
$this->session->set_userdata($data);
and retrieved the value in site.php
$data['userid'] = $this->session->userdata('username');
$this->load->view('members_area' , $data); //send the info to the controller
if you want some common functions in two controller put it in a helper file
http://ellislab.com/forums/viewthread/55212/
I am in the process of building a webapp in cakePHP where once a user has signed up they would have their own profile page that can be seen by other members. Ideally I would like a nice url e.g. www.website.com/davejones but I can settle for something that was like www.website.com/uid=235325.
What would be the best way of going about this?
Basically you would just add a profile method to your UsersController that takes the user id/username as a parameter and then just serve that page.
class UsersController extends AppController {
public function profile($uid) {
$data = $this->User->findById($uid);
$this->set(compact('data'));
}
}
Would make all data of that user (incl. associated data, if your models setup properly) go into the $data variable that you can call from your view. Do with it what you want there.
Where can I place my "global" function, which will check, if user is logged in?
Because I want to do something like: user is able to browse some pages only when the function isLogged() returns TRUE, and I'd have to use it in some views, that's why it should be a "global" function, which I can access from anywhere.
Is that possible? Or there is any better solution for this?
You should probably put it into a Library, and autoload the library. When you need to use the "logged_in" flag in a view, pass it in from the controller. Example:
application/libraries/Auth.php
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Auth
{
public function is_logged_in ()
{
// Change this to your actual "am I logged in?" logic
return $_SESSION['logged_in'];
}
}
application/config/autoload.php
...
$autoload['libraries'] = array(
...
'auth',
...
}
`application/controllers/welcome.php
<?php ...
public function index ()
{
$view_data = array
(
'logged_in' => $this->Auth->logged_in()
);
$this->load->view('my_view', $view_data);
}
application/views/my_view.php
<? echo $logged_in ? 'Welcome back!' : 'Go login!' ?>
Are you using an authentication library? If not I'd suggest one.
They come with functions like that.
Tank Auth for example has: is_logged_in() , which does exactly what you want!
http://www.konyukhov.com/soft/tank_auth/
For more info about which library to use you should check out this answer which compares all libs:
https://stackoverflow.com/a/476902/576223
If you don't want an authentication library you can do as suggested by Joe
you can use MY_controller with all function needed on every page of your website. and inherit all controllers from it. read this oficial wiki
In CakePHP we can use $this->Auth->allow('someMethod'); to make a page viewable without having to login. How do I make some the same page is not viewable when a user is logged in? An example of this would be a register page which we want to be accessible without user logged in ... but not accessible once a user logged in.
I put $this->Auth->deny('someMethod') in isAuthorized() but it seems to me that if the method is in the allow list then isAuthorized is not called when we try to run that page.
Any input? Thank you
There are no complex rules like that built into Cake Auth. You'll have to manually check for conditions like this. It's very simple though:
// Controller
function register() {
if ($this->Auth->user()) {
$this->redirect(/* somewhere else */);
}
}
Contrary to mlevits answer, you don't need to store anything in the Session, the info is readily available from the AuthComponent itself. http://book.cakephp.org/view/387/user
There's also an example how to do it by dynamically using deny(), but that's not as clear in a simple case like this IMHO. http://book.cakephp.org/view/383/deny
Also, deny() produces an error message ("You're not authorized to access this location"), which is probably not what you want for the user experience in this case.
EDIT:
Wasn't aware that CakePHP used a different syntax.
You can then use the following to set the Session variable:
$this->Session->write('user_id', '<some_user_name>');
Then use this to redirect the user if they are logged in:
if ($this->Session->check('user_id'))
{
$this->redirect('http://google.com');
}
And then to destroy a Session use:
$this->Session->destroy()
More information about CakePHP Sessions
Thanks
You can check it in method beforeFilter in AppController to allow aplication-wide check. For example:
<?php
class AppContoller extends Controller {
var $components = array('Session', 'Auth');
function beforeFilter(){
$this->Auth->allow('register', 'home');
// Check if current action allowed to access without authorization and User has login
if(array_key_exists($this->params['action'], $this->Auth->allowedActions) && $this->Auth->user()){
$this->redirect(/* somewhere else */);
}
}
}
?>
Of course you can also implements it in some controller instead of AppController.