Closed jackttcoms closed 6 years ago
Jackttcoms, Thanks for your gracious like of this repo. It pulls together techniques from several MVC's I have studied and used such as my preference towards the controller and method names in the URL taken from how CodeIgniter performs this process while still offering a URL route pattern substitute as in Laravel.
Ok thanks so much! Would you ever add the complete user authentication part of it because then i can properly see how it works etc.
Thanks again!
That will be coming in a few months as I wanted this to be a great starting point for building MVC applications. - Thanks.
Ok great thanks very much and also does this have an autoloader for the classes?
Thanks again
spl_autoload_register(function($className){ require_once 'libraries/' . $className . '.php'; });
It does and in a magical way. The spl_autoload_register() function is a great PHP item. It will only load the physical Class file when the Class->method is called from within any other script. This avoids overloading memory like other frameworks that load every Class at regardless if you need them.
Ok thats very good! My final question is will you add an error feature so it automatically goes loads the 404 page if the route does not exist + also inserts it into the error log, encryption class and does this simple MVC framework work right now? or still developing stages because otherwise would it work if i bought over this bit of code and implemented it? The link below is an OOP MVC! Would it work with this framework seeing as it looks like its almost based off of it file structure and code wise?
https://github.com/amone4/MVC-website/tree/master/app/views/users
Thanks again very much
There is a default route value in the config.php file you set as your 404-page location - see: $config['default_method'] = 'index'; (Currently set to the home page).
Logging the 404 events is not a built-in function as this would be some custom process you might like to perform, like logging then sending an email alert, etc.
My MVC is OOP as most are of this type of design pattern. It is fully functional and ready to use. You can extend it yourself with custom code, or load other packages using something like NPM, etc.
Ok i'm trying to implement the script now! I'm gonna place it in a subdirectory called 'mvc'! Where would i go to do that as right now i see its configured for the main root!
Thanks in advance!
View files (html returned to the browser) are placed in the "mvc-framework-php/app/views/" directory. In your case "mvc/app/views/". You can also create subdirectories for your paths as such "mvc/app/views/users/".
Sorry i was not clear enough! Basically i go to localhost/mvc/ and it just shows the app + public directory! Ami i missing something (configuration)?
Thanks
In your case, set up your web server to use the /mvc/public/ folder as the
oh right, is this not capable of doing subdirectories due to how the script is structured etc? Thanks again and what kind of localhost software do you recommend i use as im currently using WAMP but i'm unsure if that's the right software to use seeing as everything must be in the htdocs folder which is sub directories and as im not using it in the root i guess this error is normal?
Warning: The use statement with non-compound name 'PDO' has no effect in C:\xampp\htdocs\mvc\app\core\Model.php on line 16
Warning: The use statement with non-compound name 'PDOException' has no effect in C:\xampp\htdocs\mvc\app\core\Model.php on line 17
What if i edit this? Lik on Codeigniter its = $config['base_url'] = "http://www.domain.com/my_subfolder/"
$web_root = '/'; // where public files are served from relative to the web site.
$base_path = __DIR__; // where public files are served from relative to the server root.
$application_path = '../app/'; // default: '../app/'
$controllers_path = '../app/controllers/'; // default: '../app/controllers/'
$models_path = '../app/models/'; // default: '../app/models/'
$views_path = '../app/views/'; // default: '../app/models/'
$helpers_path = '../app/helpers/'; // default: '../app/helpers/'
Thanks
A few issues I see here:
It looks like your WAMP install does not preinstall the PDO dependancies for PHP. Something you will need to look at the WAMP docs for.
A proper/secure website only provides access to the public files (ie: /public) and the "application" remains behind the wall (ie: one parent directory up). You should review the WAMP docs to see how to configure this deeper directory... it's a common pattern.
In place of WAMP have a look at my "vagrant-bootstrap-php71-laravel54" repository and read the installation instructions to see if you can implement it. You need to install Virtual Box and Vagrant as instructed then my shell script when run will build a real LAMP server and install the Laravel framework (not needed for your project). This a better model of a production environment run locally vs WAMP (or MAMP on Apple).
I have other PHP PDO scripts running perfectly without an issue and also on my webhosting i get the error also! Could it simply be because its not in the root folder?
Ok so with Codeigniter 4 will they do the same as you or will they still give the option for a subdirectory?
Thanks
For the two PHP Warnings - PDO and PDOException - try deleting both of those Use lines from the code. Some versions of PHP toss up this warning when PHP.ini is configured to auto-include PDO.
The CodeIgniter framework works in a similar way. You will have a "parent directory" with all files and in there will be your "public" directory that we web server must point to (not the parent).
Yes that fixed the errors perfectly! Can i ask why you did that in the first place and also for Codeigniter, even for version 4 it looks like it could work with a subdirectory so if i wanted to do something similar etc where would i edit on your wonderful framework?
Here is the link to a live demo of ur MVC: http://juschewi.epizy.com/?i=1 but as you can see it shows the directory so should i add a .htaccess file something like this?
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*)$ public/$1 [L]
</IfModule>
<?php namespace Config;
use CodeIgniter\Config\BaseConfig;
class App extends BaseConfig
{
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| If this is not set then CodeIgniter will try guess the protocol, domain
| and path to your installation. However, you should always configure this
| explicitly and never rely on auto-guessing, especially in production
| environments.
|
*/
public $baseURL = '';
/*
|------------
Thanks again you are really helpful
The USE statement works with Namespacing. It was applied in the code originally to work with the latest version of PHP 7.1 - older versions could throw an exception to it.
Within my MVC try editing these lines, here: /mvc-framework-php/blob/master/public/index.php
/*
*/ $web_root = '/'; // where public files are served from relative to the web site. $base_path = DIR; // where public files are served from relative to the server root.
On Fri, May 11, 2018 at 4:23 PM, jackttcoms notifications@github.com wrote:
Yes that fixed the errors perfectly! Can i ask why you did that in the first place and also for Codeigniter, even for version 4 it looks like it could work with a subdirectory so if i wanted to do something similar etc where would i edit on your wonderful framework?
Here is the link to a live demo of ur MVC: http://juschewi.epizy.com/?i=1 but as you can see it shows the directory so should i add a .htaccess file something like this?
Options -Multiviews RewriteEngine On RewriteRule ^$ public/ [L] RewriteRule (.*)$ public/$1 [L]
<?php namespace Config; use CodeIgniter\Config\BaseConfig; class App extends BaseConfig { /* -------------------------------------------------------------------------- Base Site URL URL to your CodeIgniter root. Typically this will be your base URL, WITH a trailing slash: http://example.com/ If this is not set then CodeIgniter will try guess the protocol, domain and path to your installation. However, you should always configure this explicitly and never rely on auto-guessing, especially in production environments. / public $baseURL = ''; / |------------
Thanks again you are really helpful
ok so i cant just do a full web url?
$web_root = 'https://domain.com/test/'; // where public files are served from relative to the web site.
$base_path = __DIR__; // where public files are served from relative to the server root.
Also im no expert but on both sets of hosting i have tested it on, it does not automatically load to the public file so i think on ur github you are missing a .htaccess file in the root that has this bit of code
<IfModule mod_rewrite.c>
Options -Multiviews
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*)$ public/$1 [L]
</IfModule>
Thanks
If you get your web server to point to the /public directory (as it needs to be) there you will find /public/.htaccess which manages the rewrite conditions you think we missing.
I correct my self on the previous suggestion of setting the $web_root. Setting that value would be if you were storing your public files in a deeper subdirectory, like /public/website/
The structure of the "application framework" is that it's a framework and configuring the server is needed. Getting your web server to use /public as root is the key to making the application work.
Ok thanks! Its all working fine now and i have decided to keep it in the root instead of a subdirectory! Anyway please can you write a simple example for the model!
For example here:
public function getUsers()
{
please can you write some demo code below that bit because as im new to MVC's in general, i thought to models were for the SQL bit so do i simply put something like below because right now it looks like its the controller that is dong the sql with this bit of code so please can you explain further! ``` $bind = [':id' => $user_id]; $data = $user->select('users','id = :id', $bind);
public function updateUser($data)
{
$this->db->query('UPDATE users SET name = :name, email = :email, notes = :notes where id = :id');
// Bind values
$this->db->bind(':id', $data['id']);
$this->db->bind(':name', $data['name']);
$this->db->bind(':email', $data['email']);
$this->db->bind(':notes', $data['notes']);
// Execute
if( $this->db->execute() ){
return true;
} else {
return false;
}
}
I dont know why but its just confusing me! Do i put the database query's in the model or the controller? Like this https://github.com/amone4/MVC-website/blob/master/app/models/User.php or would you be able to write something really simple like a login! No register just login and then i go from there?
Thanks again, this MVC is so powerful i just need to learn it :)
Reference my repository docs from your install here: http://juschewi.epizy.com/docs
There are four basic sections: Routing, Models, Views, Controllers. They give a general example of use. Each of those components has detailed comments under the /app directory... read the code to learn how the functions work.
The Routes define the Controller and the method in that controller to run. The method called in the Controller should outline your "business logic" and (if required) call on methods in your Models. Models contain SQL for database access (read,write,update,delete) as well as any other type of "data models" such as access to some remote API, or a servers that makes a PDF, or Word doc, or XML. Models are for any type of data.
When the data (or file, or other) is returned to the Controller it will either 1) pass it down to the View OR 2) return it to the browser (ie: downloading a PDF file).
The View that receives the data should only do what is needed to present the data, such as PHP foreach loops over an array of data representing a report of some type.
That said, in the docs provided you will see an "alternative approach" under the Controller example where the I'm creating an object of the User Model then constructing a query against it getting a value back. This is also acceptable in a case where you query needs are minimal - to run a query in the Controller vs the Model.
The Model classes extend the core "Model" which is an "abstraction layer" in place of writing pure SQL. This adds a layer of security against SQL-injection attacks.
Just remember.... Routes define which Controller (and Method) to call. Controllers might access a Model for some data, but will generally always return a View to render in the Browser.
Ok thanks that cleared it up perfectly! That confused me for some reason regarding this
This is also acceptable in a case where you query needs are minimal - to run a query in the Controller vs the Model.
Thanks again and do u plan to make a simple app anytime soon using the framework such as a blog?
P.S. I know why i also made a mistake! I was looking at the documentation on Github not the doc that comes with it so i missed a bit of it!
Hi this looks really good so can i ask whether this is based on the Udemy course? https://www.udemy.com/object-oriented-php-mvc/
Thanks