<?php
class Users extends Controller
{
public function __construct()
{
$this->userModel = $this->model('User');
$this->postModel = $this->model('Post');
}
public function index()
{
if(!isLoggedIn() ){
redirect('users/login');
}
$userDetails = $this->userModel->getUserById($_SESSION['user_id']);
$users = $this->userModel->getUsers();
$data = [
'users' => $users,
'role' => $userDetails
];
return $this->view('users/index', $data);
}
}
Here is my model code
public function getUserById($id)
{
$this->db->query('SELECT * FROM users WHERE id = :id');
// Bind values
$this->db->bind(':id', $id);
return $this->db->single();
}
Iām very confused with how i get the data across but individually and globally so please can someone tell me what iām doing wrong as i started to learn MVC 3 days ago but i cant figure out how to get certain data suchg as the userDetails! So right now all im trying to show is the role id which is 2 but i just get errors! So can someone say what iām doing wrong as why the MVC bit is not working.
This bit of code works fine: <?php foreach($data['users'] as $user) : ?> and loads in everything i need but this is for all users im trying to load individual users data in but i keep getting errors etc!
public function getUserById($id)
{
$this->db->query('SELECT * FROM users WHERE id = :id');
// Bind values
$this->db->bind(':id', $id);
return $this->db->single();
}
Am i doing something wrong? Im trying to simply get the $data['snguser'] to equal a variable so i can do $userDetails->role; but its not working and for some reason its really confusing me! Am i doing arrays wrong or something?
Thanks very much
New error message:
Notice: Undefined variable: userDetails in C:\xampp\htdocs\framework\app\views\admin\index.php on line 33
Notice: Undefined variable: userDetails in C:\xampp\htdocs\framework\app\views\admin\index.php on line 35
Notice: Trying to get property 'role' of non-object in C:\xampp\htdocs\framework\app\views\admin\index.php on line 35
Did you intend this line to be 'userDetails' => $userDetails? It would explain why $userDetails is undefined in your view, because youāre actually assigning it to the name āroleā.
but unfortunately it still wonāt work and i keep getting the same error!
Notice: Undefined variable: userDetails in C:\xampp\htdocs\framework\app\views\admin\index.php on line 33
Notice: Undefined variable: userDetails in C:\xampp\htdocs\framework\app\views\admin\index.php on line 35
Notice: Trying to get property 'role' of non-object in C:\xampp\htdocs\framework\app\views\admin\index.php on line 35
The thing that is so confusing tho is that this bit of code here works fine
As iām very new to MVC i thought id check out Udemy so i created a simple MVC from a tutorial but now im stuck with trying to do user roles because i could do sessions etc but peoples roles change so its more convenient just automatically changing rather than having to logout and then log back in again etc
@jack55,
I had a quick look at the Udemey and could not find the particular tutorial. Is the tutorial free and what is the exact link?
Meanwhile I downloaded the GitHub MVC-Framework and managed to get most stuff working locally.
Unfortunately I am in the process of updating to the latest Linux 18.04 Bionic Beaver and have had a tremendous amount of problems. I have managed to use FileZilla to copy the files onto the remote server but unable to upload the database because I am locked out of PhpMyAdmin. I think the problem is the new SSH Keys. I will try reinstalling PhpMyAdmin tomorrow.
Meanwhile take a look at the Home and About pages because I have included an image which shows the MVC structure and how the Http requests are passed from the view to the controller, maybe cache, model etc It will be useful to trace back which files are causing your display errors.
Is it possible on an MVC to have a global bit of code that is always there for all controllers? otherwise it will have to be individual which is okay but im wondering whether there is a more sleek way!
And the second one is how would you recommend i integrate a roles and permissions into this MVC sleekly and easily!
Iām looking at these tutorials and basically i just want to know the best way to implement them so it sort of goes with the above question regarding a global controller etc?
Finally with the view controller it looks like this so right now it loads in pages/index.php straight away as the index.php but then when you go to other pages such as about it goes pages/about.php so is there a way to say if its pages then allow the link to work without pages so it just goes name.com/about like it does with just name.com which loads the pages/index?
<?php
/*
* App Core Class
* Create URL & load core controller
* URL FORMAT - /controller/method/params
*/
class Core
{
protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];
public function __construct()
{
$url = $this->getUrl();
// Look in controllers for first value
if (file_exists('../app/controllers/' . ucwords($url[0]) . '.php')) {
// If exists, set as controller
$this->currentController = ucwords( $url[0] );
// Unset 0 url
unset($url[0]);
}
// Require the controller
require_once '../app/controllers/' . $this->currentController . '.php';
// Instantiate controller class
$this->currentController = new $this->currentController;
// Check for second part of url
if (isset($url[1])) {
if(method_exists($this->currentController, $url[1]))
{
$this->currentMethod = $url[1];
unset($url[1]);
}
}
// Get params
$this->params = $url ? array_values($url) : [];
//Call a callback with array of params
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl()
{
if (isset($_GET['url'])) {
$url = rtrim($_GET['url'],'/');
$url = filter_var($url,FILTER_SANITIZE_URL);
$url = explode('/',$url);
return $url;
}
}
}
Thanks ever so much and i know this is alot of questions but iām just trying to learn a better and more secure way to code and i below MVC is the first step! I now understand around 80% of MVC just the few bits i dont understand are the bits limiting me as all mentioned above! Thanks again
Q1. Is it possible on an MVC to have a global bit of code that is always there for all controllers?
Yes check out the videos about class Pages extends Controller
Q2. And the second one is how would you recommend i integrate a roles and permissions into this MVC sleekly and easily!
How far have you progressed with the tutorials? Have you created the database tables and classes?
Q3. allow the link to work without pages so it just goes name.com/about like it does with just name.com which loads the pages/index?
Yes check out .htaccess file and also the way the controllers handle the pages URI.
I managed to get the demo site running and it has the Flowchart image which makes it easier to visualise what happens with a page link request. Link in Post #8
Are you talking about the course? Did you buy it also?
I have reached the end and am now doing custom work on the MVC so i have created a profile page, change password and show all users. Now i donāt know where to start regarding implementing roles efficiently and seemlessly.
I have John and it goes like this call_user_func_array([$this->currentController, $this->currentMethod], $this->params); but iām trying to find the best way to make it like an array so that its almost like this (a quick mockup)
No I did not buy the course, I just looked at the sample videos. I did download the provided link from GitHub and updated my web-page. It was hard going trying to eliminate /pages/ from the URI but eventually I managed. The script is OK to learn MVC basics but very limited compared with the features available with CodeIgniter. I cannot expect many updates and would recommend using the skills gained to test other PHP Frameworks which are regularly maintained with security updates.
I have no knowledge of using roles. The tutorial links seem comprehensive and I think it is only a matter of spending some time trying to integrate the scripts into your current project.
What is Discord? I tried searching and could not find a satisfactory solution.
[off-topic]
Discord is an application used by groups for messaging, sharing information and resources. I belong to a web development meet-up that makes use of a Discord account. Itās at discordapp.com
[/off-topic]
It was quite involved and a lot of the script had to be rewritten, navbar.php had to have the pages/ removed and also Core.php required this additional script:
# Check if second part of url is set (method)
if(isset($url[1]))
{
// Check if method/function exists in current controller class
if(method_exists($this->currentController, $url[1]))
{
# fred($url, __line__);
# Set current method if it exsists
$this->currentMethod = $url[1];
// Unset 1 index
unset($url[1]);
}
}
if(isset($url[0]))
{
# fred($url, __line__);
// Check if method/function exists in current controller class
if(method_exists($this->currentController, $url[0]))
{
// Set current method if it exsists
$this->currentMethod = $url[0];
// Unset 1 index
unset($url[0]);
}
}
# Get params - Any values left over in url are params
# DEFAULT
# controller -> Pages.php
# params -> about
$this->params = $url ? array_values($url) : [];
]// end public function __construct()
I introduced this script into index.php which helped tremendously:
It took quite some time but I was determined to finish. Now looking to return t CodeIgniter because it is so much easier. Core models do not require modifications.
Have you an online version or just working locally?
iām working locally atm but im planning on moving to Codeigniter once version 4 is out! I dislike Laravel due to the composer requirement and i just prefer the simplicity of Codeigniter 10x but what im building right now is a simple blog system that does not need the complexities of a proper framework which is why iām doing my own!
Also after all the hard work you did getting the pages to work, do you think it would of been easier for you to do an array class that does custom routing?
I have had CI4 running successfully using an online server with a free domain for quite a few months. MySql and Ajax works without any problems and I believe the delay in releasing the beta version is having to implement other database systems. As far as working locally is concerned I usually find that uploading to a remote server introduces inconsistencies. These are far better to be resolved asap before they escalate requiring drastic changes to ensure the the script works remotely.
Did you have any success with implementing your custom array class solution?
Regarding the custom routes option i unfortunately did not manage to get it working but i believe im kind of close in the sense that im like 15% getting there! Right now I had to change quite a lot which i think is a bad thing and im sure you probs would do it in like two lines of code seeing as you seem much more experienced than me but basically at the end of the day i want it to work the way it is right now and also a custom route as well if i want to shorten it etc.
Here is the routes.php file:
$route['default'] = 'home';
$route['user/([a-z]+)/([a-z]+)'] = 'home/user/$1/$2';
$route['page/([a-z]+)/([a-z]+)'] = 'admin/my-admin/page/$1/$2';
$route['anything/:any/:any'] = 'home/index/$1/$2';
$route['number/:num/:num'] = 'home/index/$1/$2';
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
//A URL with āproductā as the first segment, and a number in the second will be remapped to the ācatalogā controller class and the āproduct_lookup_by_idā method passing in the match as a variable to the method.