Individual Profile / User Page in CakePHP - php

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.

Related

Creating user profile tables that are publicly accessible in Laravel 5.8

I am having some trouble with my application when trying to create the user profiles. Here is the issue:
I am trying to create a view called userprofile.blade.php which will output any given users profile (based on id or username...doesn't really matter right now). Each profile page will show name, description, location, profile pic, and the given users posts. I used Laravel's Make:auth command to create the necessary authentication, customized the authentication forms, and then migrated all the columns I needed in my database.
My create and update methods work just fine (registering new users and updating their information). All the information is saved correctly in the database. However, I can only access it in my views with {{Auth::user()->}}. Whenever I try to use the Model in my Controller to access the data I need, it doesn't work. It seems to me as though I need to separate Laravel's default 'User' model with a custom model which I would call 'Account' or something along those lines.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;
use App\Recipe;
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth', ['except' => [
'index', 'show'
]]);
}
public function index(){
$user = User::find($id);
return view('user.userprofile')->with('user');
}
I only included the index method from my UserController to keep it simple. It breaks down and tells me that 'id' in $user = User::find($id); is an undefined variable. That tells me that it isn't accessing my database table.
My question is, should I create a new fresh model that isn't mixed up with authentication to handle all the user profile information? If so, how do I access my current database table 'users' from this new model?
Please let me know if I need to clarify things for you guys. I'm not very experienced and I understand if my question is fuzzy. Thanks so much for your time!! I really appreciate any help I can get!
Hello and welcome to developing with Laravel!
You're on the right track - you need to identify which user's profile you're viewing, retrieve it from the database, and pass it to the Blade view. You don't need a new model or anything, though! Just need to complete what you've started.
You should start by defining a route parameter in your route, that will capture the dynamic data you want from the URL. Let's use the numeric ID for now. If you want a URL that looks like example.com/user/439, your route should look something like Route::get('user/{id}', 'UserController#index');.
Once you have that, the id parameter will get passed to your controller's method. Define it as a method parameter, and it'll be usable: public function index($id) { ... }
I think you can take it from there. :)

Codeigniter multiple controllers vs many methods?

I have a site that has a lot of pages that lye at the root (ex. /contact, /about, /home, /faq, /privacy, /tos, etc.). My question is should these all be separate controllers or one controller with many methods (ex. contact, about, index within a main.php controller )?
UPDATE:
I just realized that methods that are within the default controller don't show in the url without the default controller (ie. main/contact wont automatically route to /contact if main is the default controller ). So you would need to go into routes and override each page.
If all of these are just pages, I would recommend putting them into a single controller. I usually end up putting static pages like this into a 'pages' controller and putting in routes for each static page to bypass the '/pages' in my URLs.
If they are share the same functionality, so they should be in the same controller.
for example, if all of them are using the same model to take content from, so, one controller can easily handle it.
Why in one controller? because you always want to reuse your code.
class someController{
function cotact(){
print $this->getContentFromModel(1);
}
function about(){
print $this->getContentFromModel(2);
}
function home(){
print $this->getContentFromModel(3);
}
private function getContentFromModel($id){
return $this->someContentModel->getContentById($id);
}
}
(instead of print, you should use load a view)
See in my example how all of the function are using the same getContentFromModel function to share the same functionality.
but this is one case only, there could be ther cases that my example can be bad for...
in application/config/routes.php
$route['contact'] = "mainController/contact";
$route['about'] = "mainController/about";
$route['home'] = "mainController/home";
$route['faq'] = "mainController/faq";
$route['privacy'] = "mainController/privacy";
and you should add all of these methods within the mainController.php
You can also save the content of the pages in your database, and them query it. For instance, you can send the url as the keyword to identify the page content
$route['contact'] = "mainController/getContent/contact";
$route['about'] = "mainController/getContent/about";
$route['home'] = "mainController/getContent/home";
$route['faq'] = "mainController/getContent/faq";
$route['privacy'] = "mainController/getContent/privacy";
in this case you only have to create one method named "getContent" in the controller "mainController" and this method will look something like this:
class mainController extends CI_Controller
{
public function getContent($param)
{
$query = $this->db->get_where('mytable', array('pageName' => $param));
// then get the result and print it in a view
}
}
Hope this works for you
The page names you listed should probably be different methods inside your main controller. When you have other functionality that is related to another specific entity, like user, you can create another controller for the user entity and have different methods to display the user, update the user, register the user. But its all really a tool for you to organize your application in a way that makes sense for your domain and your domain model.
I've written a blog post about organizing CodeIgniter controller methods that might be helpful to you. Check it out here: http://caseyflynn.com/2011/10/26/codeigniter-php-framework-how-to-organize-controllers-to-achieve-dry-principles/

how to prevent entering to the site using url typing in codeigniter

I have a site using CodeIgniter that is almost complete now. My problem is that, even though I have implemented sessions and maintain a login system, a person can access any page by typing the URL into the browser address bar.
I have implemented the session for patient registration like this:
function index(){
$this->is_logged_in();
}
function log_out(){
$this->session->sess_destroy();
redirect('login_controller');
}
function is_logged_in(){
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in)||$is_logged_in!= TRUE ){
redirect('login_controller');
}else{
$this->main();
}
}
Anonymous users can't acess the system just by typing the controller name like this:
http://localhost/demo_site/index.php/register_controller
But they can do it like this:
http://localhost/demo_site/index.php/register_controller/search_patient
Person can't access by typing the controller name, but can enter the system by typing a longer url than the controller, like the one shown above.
What is the problem here? What are the possible solutions??
You will have to implement a login check in the controller's constructor.
Whenever the controller is called, it should check if the user is logged in - if they are not, redirect to a login page or an error page.
To confirm if it is entering the login check put an echo and exit inside the is_logged_in() function and check if it appears in case of http://localhost/demo_site/index.php/register_controller/search_patient
You are probably doing login check in your respective modules and thus you missed for some cases.
It is better to define a set of private modules (say in an array) and do the login check in the frontcontroller itself (in one place) instead of repeatedly in module level.
Sounds like a routing problem. You need to set up your routes to make the second case illegal or at least map to the same controller as the first case. More on routing here.
I agree with tHeSiD. This code should go in the constructor. Ideally in a base class which you use to extend all admin related or restricted classes with. Normally I use an Admin_Controller base class that extends CI_Controller (2.0) or Controller (1.7.x) and then create my application controllers by extending the Admin Controller.

Having actions/pages without requiring a login

I am building an application using cakePHP. Do we have a method where we can allow public users access to certain pages without logging in. There would be a few pages such as about us regarding the whole organisation or a contact us page. Is there a method to avoid login access, something similar to how we have ways to add components or set layouts.
As Martin Bean says, you can use ACL. For a sophisticated site, that would be my choice. You do not have to be logged in to access the public pages. http://multiheadweighers.co.uk is an example of a site that uses ACL. There is a fully featured CMS behind the public pages.
For a simple site I would allow access to, for instance, the view action using
function beforeFilter() {
parent::beforeFilter;
$this->Auth->allow('view');
}
see: http://book.cakephp.org/view/1257/allow
It really isn't a big deal - try it and you'll see how easy it is.
EDIT:
From the book # http://book.cakephp.org/view/1550/Setting-up-permissions
Now we want to take out the references
to Auth->allowedActions in your users
and groups controllers. Then add the
following to your posts and widgets
controllers:
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allowedActions = array('index', 'view');
}
This removes the 'off switches' we put in earlier on the users and groups controllers, and gives public access on the index and view actions in posts and widgets controllers. In AppController::beforeFilter() add the following:
$this->Auth->allowedActions = array('display');
This makes the 'display' action public. This will keep our PagesController::display() public. This is important as often the default routing has this action as the home page for you application.
EDIT 2:
$user = ($this->Auth->user())?$this->Auth->user():'Anonymous';
if(!$this->Acl->check($user,"{$url}"))
$this->redirect($this->referer()); // or whatever action you want to take.
The solution would be to use the allow method in the Auth component to let the user visit those pages.
Thank you!

Making a controller for home page

Can you tell me how to use a controller for home page because i'm trying to put a model's data in home.ctp (homepage view) with
<?php $this->user->find() ?>but it returns
Notice (8): Undefined property:
View::$user [APP\views\pages\home.ctp,
line 1]
You should check out the cookbook; it has some solid CakePHP tutorials, at http://book.cakephp.org/
You haven't really provided alot of information, but my guess is your Controller uses a model 'User', and you're putting $this->user->find() in your view, when it should be in your controller. In your controller's action you'll want/need to do something like this...
Users_Controller extends AppController {
function index() {
$arrayOfUsers = $this->User->find(...);
$this->set('users', $arrayOfUsers);
}
}
You can then - in your View - access 'users' like so:
pre($users);
... since you used the Controller method set() to send a variable $users to the view.
All you really need to do is create a new controller if that's the direction you want to go. If this is the only statement you have that requires data access, it might be worth faking it in only this method of the PagesController. For example, one of my projects' homepages is 99% static save for a list of featured events. Rather than move everything out to a new controller or even loading my Event model for the entire PagesController (where it's not needed), I just applied this solution in PagesController::home():
$attractions = ClassRegistry::init ( 'Attraction' )->featured ( 10 );
Works great. If your page is more dynamic than mine, though, it may well be worth routing your homepage through a different controller (one that is more closely related to the data being displayed).
The default controller for the home page i.e home.ctp view, is located in /cake/libs/controller/pages_controller.php. This controller can be overriden by placing a copy in the controllers directory of your application as /app/controllers/pages_controller.php. You can then modify that controller as deem fit i.e. use models, inject variables to be used in the home page etc

Categories