RE: [PHP] Entry point of an MVC framework

2012-07-13 Thread Adam Nicholls


> -Original Message-
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: 12 July 2012 21:21
> To: php-general@lists.php.net
> Subject: [PHP] Entry point of an MVC framework
> 
> Hi,
> 
> I have started to develop a simple MVC framework.
> 
> I have a base controller class which is abstract and all of the controllers 
> are
> inherited from that. Every controller contains actions represented by
> methods. (E. g. there is a controller for managing product items in a
> webshop, and there are seperate actions for create, modify, remove, etc.)
> There is also a default action (usually an index page), which is used when
> nothing is requested.
> 
> But what is the best way to invoke an action? I can't do it with the
> baseController constructor, becouse parent class can't see inherited classes.
> And I can't do it with the constructor of the inherited class, becouse this 
> way I
> would overwrite the parent constructor. And as far as I know, it is not a good
> practice to call a method outside of the class, becouse the concept of
> operation of the class should be hidden from the other parts of the
> application.
> 
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php

Hi Simon,

You'll probably want to look at Bootstrapping your framework too. Ideally the 
bootstrap will include (or have an autoload function) to load your classes - 
controllers/models/views, and it'll also deal with routing so deciding which 
controller and action to call based on the URL, this usually involves doing an 
explode() on the $_SERVER['QUERY_STRING'], and using Apache's mod_rewrite to 
forward all requests to your bootstrap (often named index.php)

Building frameworks and going through the motions are a great way to build up 
experience and play with areas of the language you might not normally use, you 
might want to get inventive with the Reflection class to check actions or 
controllers exist and then forward the user to a 404 page later on.

Cheers
Adam.

=

This email is intended solely for the recipient and is confidential and not for 
third party unauthorised distribution. If an addressing or transmission error 
has misdirected this email, please notify the author by replying to this email 
or notifying the system manager (online.secur...@hl.co.uk).  If you are not the 
intended recipient you must not disclose, distribute, copy, print or rely on 
this email. 

Any opinions expressed in this document are those of the author and do not 
necessarily reflect the opinions of Hargreaves Lansdown. In addition, staff are 
not authorised to enter into any contract through email and therefore nothing 
contained herein should be construed as such. Hargreaves Lansdown makes no 
warranty as to the accuracy or completeness of any information contained within 
this email. In particular, Hargreaves Lansdown does not accept responsibility 
for any changes made to this email after it was sent. 

Hargreaves Lansdown Asset Management Limited (Company Registration No 1896481), 
Hargreaves Lansdown Fund Managers Limited (No 2707155), Hargreaves Lansdown 
Pensions Direct Limited (No 3509545) and Hargreaves Lansdown Stockbrokers 
Limited (No 1822701) are authorised and regulated by the Financial Services 
Authority and registered in England and Wales. The registered office for all 
companies is One College Square South, Anchor Road, Bristol, BS1 5HL. 
Telephone: 0117 988 9880

__
This email has been scanned by the Symantec Email Security.cloud service.
For more information please visit http://www.symanteccloud.com
__


Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Matijn Woudt
On Fri, Jul 13, 2012 at 12:41 AM, Paul M Foster  wrote:
> On Fri, Jul 13, 2012 at 12:26:33AM +0200, Timmy Sjöstedt wrote:
>
>
> [snip]
>
>>
>> The Controllers also doesn't care if the data is stored in MySQL,
>> Postgres, in RAM or on butterflies or anything else, as long as data
>> is returned in an expected format.
>
> I can attest that attempting to store data on butterflies is
> exceptionally difficult. You really want to isolate that in your models.
> Fortunately, the herding_cats design pattern works equally well with
> butterflies.
>
> (Welcome to Friday, half a day early! ;-)
>
> Paul
>

Well Paul, It's Friday over here!:)

Thinking about the butterflies, you could use them to store data (not
ON them). Just put a butterfly in either the left or the right box,
and you have a 1 bit storage. Just one problem, you never know how
long it will take the butterfly to fly from the left to the right, or
the other way around when the gate is open..

- Matijn

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Paul M Foster
On Fri, Jul 13, 2012 at 12:26:33AM +0200, Timmy Sjöstedt wrote:


[snip]

> 
> The Controllers also doesn't care if the data is stored in MySQL,
> Postgres, in RAM or on butterflies or anything else, as long as data
> is returned in an expected format.

I can attest that attempting to store data on butterflies is
exceptionally difficult. You really want to isolate that in your models.
Fortunately, the herding_cats design pattern works equally well with
butterflies.

(Welcome to Friday, half a day early! ;-)

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Paul M Foster
On Thu, Jul 12, 2012 at 02:44:12PM -0700, Daevid Vincent wrote:

> > -Original Message-
> > From: Simon Dániel [mailto:simondan...@gmail.com]
> > Sent: Thursday, July 12, 2012 1:21 PM
> > Subject: [PHP] Entry point of an MVC framework
> > 
> > I have started to develop a simple MVC framework.
> 
> Yeah! Just what PHP needs, another MVC framework
> 
> NOT.

And here's why Daevid is right. I've been having to work with Symfony, a
well-known framework. Nothing against Symfony, per se. It's much like
other frameworks. However, here's the kicker-- if you make an unknowing
mistake with Symfony (feed it the wrong data at the wrong time), you get
the most beautiful 23-file deep stack trace. Trying to track down why
this parameter (which I didn't even give Symfony) is making it barf is
pure torture. I'm having to wade through deep Symfony code to figure it
out. Object after object instantiating other objects, magic methods,
virtual classes, design pattern classes, etc. I didn't write any of this
code, but I've got to wade through 23 files of it to figure out what the
heck went wrong. And, oh by the way, if you don't call ./symfony cc
(which clears its internal cache) at the right time, you're screwed and
you'll never know why.

Don't get me wrong. MVC is a great idea. It's a good way to divvy up
your code. But frameworks which enforce the paradigm have a way of
getting carried away with themselves. I think a lot of the people who
write them try too hard to impress other programmers with the cleverness
of their designs. By the time they're done, there are hundreds or
thousands of files involved. Every page load has to call in a
significant portion of those files, full of deep, obscure classes. Sure
*looks* kewl. But it's about 15 levels deeper than it has to be.
Remember, this is the web, folks. Your "application" has a lifetime of
one page load, unlike the platform-native accounting program, which
you're liable to keep open on your desktop all day.

I prefer to develop shallow classes which deal with specific aspects of
page handling (dates, databases, encryption, users, etc.). Then use
those as part of the "toolkit", assembled (again on a shallow basis) in
a loosely MVC pattern.

If I make a mistake, I should be able to isolate where it is within an
hour (ideally much less). And be able to go to the specific class and
method involved.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Timmy Sjöstedt

On 07/12/2012 10:21 PM, Simon Dániel wrote:

Hi,

I have started to develop a simple MVC framework.

I have a base controller class which is abstract and all of the
controllers are inherited from that. Every controller contains actions
represented by methods. (E. g. there is a controller for managing
product items in a webshop, and there are seperate actions for create,
modify, remove, etc.) There is also a default action (usually an index
page), which is used when nothing is requested.

But what is the best way to invoke an action? I can't do it with the
baseController constructor, becouse parent class can't see inherited
classes. And I can't do it with the constructor of the inherited
class, becouse this way I would overwrite the parent constructor. And
as far as I know, it is not a good practice to call a method outside
of the class, becouse the concept of operation of the class should be
hidden from the other parts of the application.



By reading your description it sounds like you are using a "C" pattern 
rather than the "MVC" pattern.


For example you are saying that "there is a controller for managing
product items in a webshop". This should be the job of a Model, not a 
Controller.


MVC is generally the following:

Model is responsible for data.
View is responsible for presentation of data.
Controller is responsible for connecting Model and View.

In web applications there is also usually some kind of URL Router in 
front of the Controller.


For example when the framework receives a request to "/items/get_all", 
the get_all method in ItemsController is executed.


get_all will proceed by loading an ItemModel and executing getAll on it, 
which returns an array of items.


The controller now loads a new View and passes the array of items to it, 
which is then rendered as HTML and send to the browser, or whatever the 
View wants to do.


(If the Model didn't return what we expected, the Controller would load 
another View that simply contains "I'm a teapot!" or "No items available 
in this category!")


This is great, because now we can have multiple Controllers accessing 
the same data without having to inherit or duplicate code (DRY).


The Controllers also doesn't care if the data is stored in MySQL, 
Postgres, in RAM or on butterflies or anything else, as long as data is 
returned in an expected format.


The View can of course be changed to another to easily change the 
appearance (representation) of a resource.


I'd suggest reading some more about MVC before going any further, and 
other design patterns wouldn't harm either :-)



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Entry point of an MVC framework

2012-07-12 Thread admin
-Original Message-
From: Timmy Sjöstedt [mailto:m...@iostream.se] 
Sent: Thursday, July 12, 2012 6:01 PM
To: php-general@lists.php.net
Subject: Re: [PHP] Entry point of an MVC framework



On 07/12/2012 11:44 PM, Daevid Vincent wrote:
>> -Original Message-
>> From: Simon Dániel [mailto:simondan...@gmail.com]
>> Sent: Thursday, July 12, 2012 1:21 PM
>> Subject: [PHP] Entry point of an MVC framework
>>
>> I have started to develop a simple MVC framework.
>
> Yeah! Just what PHP needs, another MVC framework
>
> NOT.
>
> Why are you re-inventing the wheel?
>
> Personally I *hate* frameworks with a passion, but if you're going to 
> use one, then why not just build with one that is already out there 
> and well supported. http://www.phpframeworks.com/ to start with.
>
>

I would say this is a perfectly sane idea if one wants to learn how things
actually work.

Most frameworks, however, are very bloated because they have to support and
implement many things, as they are general purpose frameworks used by
everybody and their dogs.

If you only want what you need, and you know how to do it, rolling your own
system is no problem.

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php

-

Timmy,
Thank GOD someone said it. 99.999% of every MVC framework you can
download is crammed to the hilt with BLOAT. I also agree that
knowing/understanding a MVC framework is vital and sometimes the only way to
learn is to recreate the madness you see in front of you.
I like many people have created my OWN MVC framework customized to my
madness and NO BLOAT!!

I do suggest if you're starting out in MVC frameworks. You have a better
understand of .htaccess and or web.conf for better control and flexibility.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Timmy Sjöstedt



On 07/12/2012 11:44 PM, Daevid Vincent wrote:

-Original Message-
From: Simon Dániel [mailto:simondan...@gmail.com]
Sent: Thursday, July 12, 2012 1:21 PM
Subject: [PHP] Entry point of an MVC framework

I have started to develop a simple MVC framework.


Yeah! Just what PHP needs, another MVC framework

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.




I would say this is a perfectly sane idea if one wants to learn how 
things actually work.


Most frameworks, however, are very bloated because they have to support 
and implement many things, as they are general purpose frameworks used 
by everybody and their dogs.


If you only want what you need, and you know how to do it, rolling your 
own system is no problem.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Paul M Foster
On Thu, Jul 12, 2012 at 10:21:21PM +0200, Simon Dániel wrote:

> Hi,
> 
> I have started to develop a simple MVC framework.
> 
> I have a base controller class which is abstract and all of the
> controllers are inherited from that. Every controller contains actions
> represented by methods. (E. g. there is a controller for managing
> product items in a webshop, and there are seperate actions for create,
> modify, remove, etc.) There is also a default action (usually an index
> page), which is used when nothing is requested.
> 
> But what is the best way to invoke an action? I can't do it with the
> baseController constructor, becouse parent class can't see inherited
> classes. And I can't do it with the constructor of the inherited
> class, becouse this way I would overwrite the parent constructor. And
> as far as I know, it is not a good practice to call a method outside
> of the class, becouse the concept of operation of the class should be
> hidden from the other parts of the application.

While I have a progressively growing dislike of frameworks, I'll give
you the following advice from what I've observed in other frameworks:

Normally, you build a controller class elsewhere which inherits from the
base controller class. Let's say it's a controller for blog entries.
Now, you populate the (child) controller with the methods you'll need
for each action. Like a method to delete blog entries. A method for
adding blog entries. Etc.

At that point, you have to decide if you're going to use server
redirection or standard URLs to get to your controller. That is:

http://mysite.com?controller=blog&action=add

or

http://mysite.com/blog/add/

The latter method is preferred by those who believe there's such a
mythical beast as "pretty URLs" preferred by search engines (there
isn't). It requires the use of .htaccess files and Apache's mod_rewrite,
which you may not have in a shared hosting environment.

Hopefully that helps.

Paul

-- 
Paul M. Foster
http://noferblatz.com
http://quillandmouse.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Entry point of an MVC framework

2012-07-12 Thread Simon Griffiths
I totally disagree with this!   (more exclamation marks) ! (see)
!!! (and some more) !

Learning how to put design patterns into practice in your chosen language is
a great skill to have as a programmer.  I personally use various frameworks,
however the existence of them does not stop me wanting to build my own,
learn my own way and face the problems of other framework designers and
attempt to understand the problem and how it was solved.  

To me MVC is one of those and cracking it on your own is a great way to
continue learning the language and all it has to offer.

-Original Message-
From: Daevid Vincent [mailto:dae...@daevid.com] 
Sent: 12 July 2012 22:44
To: php-general@lists.php.net
Subject: RE: [PHP] Entry point of an MVC framework

> -Original Message-
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: Thursday, July 12, 2012 1:21 PM
> Subject: [PHP] Entry point of an MVC framework
> 
> I have started to develop a simple MVC framework.

Yeah! Just what PHP needs, another MVC framework

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.


--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Robert Williams
On 7/12/12 14:44, "Daevid Vincent"  wrote:


>Personally I *hate* frameworks with a passion, but if you're going to use
>one, then why not just build with one that is already out there and well
>supported. http://www.phpframeworks.com/ to start with.

I wouldn't suggest most people try to build one to actually use, but that
said, building one for fun is an excellent way to hone your skills,
especially if you're one of those people who just can't seem to come up
with an idea for something better to build that would not only hone your
skills but also be useful to the world :-).


-Bob

--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Entry point of an MVC framework

2012-07-12 Thread Ashley Sheridan
On Thu, 2012-07-12 at 14:44 -0700, Daevid Vincent wrote:

> > -Original Message-
> > From: Simon Dániel [mailto:simondan...@gmail.com]
> > Sent: Thursday, July 12, 2012 1:21 PM
> > Subject: [PHP] Entry point of an MVC framework
> > 
> > I have started to develop a simple MVC framework.
> 
> Yeah! Just what PHP needs, another MVC framework
> 
> NOT.
> 
> Why are you re-inventing the wheel?
> 
> Personally I *hate* frameworks with a passion, but if you're going to use
> one, then why not just build with one that is already out there and well
> supported. http://www.phpframeworks.com/ to start with.
> 
> 


A framework is a little more complex than a wheel, much more like the
whole car, and as any real car enthusiast will tell you, it's a lot of
fun building a car from parts :p
-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk




RE: [PHP] Entry point of an MVC framework

2012-07-12 Thread Daevid Vincent
> -Original Message-
> From: Simon Dániel [mailto:simondan...@gmail.com]
> Sent: Thursday, July 12, 2012 1:21 PM
> Subject: [PHP] Entry point of an MVC framework
> 
> I have started to develop a simple MVC framework.

Yeah! Just what PHP needs, another MVC framework

NOT.

Why are you re-inventing the wheel?

Personally I *hate* frameworks with a passion, but if you're going to use
one, then why not just build with one that is already out there and well
supported. http://www.phpframeworks.com/ to start with.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Entry point of an MVC framework

2012-07-12 Thread Robert Williams
On 7/12/12 13:21, "Simon Dániel"  wrote:


>And I can't do it with the constructor of the inherited
>class, becouse this way I would overwrite the parent constructor.

Just call to the parent constructor from the child:

public function __construct() {
   parent::__construct();
   //do whatever
}

You can call it at any point, so you could do some setup stuff and then
call it, or call it and then do more setup stuff. Basically, you get to
augment the functionality of the parent constructor.

>And as far as I know, it is not a good practice to call a method outside
>of the class, becouse the concept of operation of the class should be
>hidden from the other parts of the application.

Calling methods of other classes is quite normal, and in fact, necessary
in most cases. The trick is that each class specifically decides which
methods it wants to allow outside code to call, which it wants only
children classes to call, and which should only be called internally.
Classes do this by declaring methods as public, protected, or private,
respectively.



What you do want to aim for is a stable set of public methods, which means
that the names of existing methods and the parameters they take don¹t
change when you update the class. If you maintain this stability, then you
can make all the updates you want but not have to update any other code
using the classes. If you change the public methods (that is, change the
class API), then you'll break other code that's using those methods.

Another consideration, and one which you're also touching on, is that of
handling dependencies. If your class requires some other class to
function, you ideally should hand it an instance of the other class,
versus directly instantiating it. You can do this in a variety of ways,
but the most common are passing the object as a parameter to the class
constructor and passing it via an injection method. What you gain by doing
this is that if you want to change the behavior of the class (e.g., by
passing it an object that sends a message by SMS instead of the one it
previously used that sent messages by e-mail), it's a simple matter of
passing a different type of object that has the same public API. This also
makes testing quite a bit easier, since you can pass mock objects that
just pretend to do the functionality of the real ones, thus allowing you
to test the main class without worrying about whether all the secondary
classes upon which it relies will break anything. When you're ready to
learn more about this, do a Google search for "php (inversion of control)".

-Bob

--
Robert E. Williams, Jr.
Associate Vice President of Software Development
Newtek Businesss Services, Inc. -- The Small Business Authority
https://www.newtekreferrals.com/rewjr
http://www.thesba.com/







Notice: This communication, including attachments, may contain information that 
is confidential. It constitutes non-public information intended to be conveyed 
only to the designated recipient(s). If the reader or recipient of this 
communication is not the intended recipient, an employee or agent of the 
intended recipient who is responsible for delivering it to the intended 
recipient, or if you believe that you have received this communication in 
error, please notify the sender immediately by return e-mail and promptly 
delete this e-mail, including attachments without reading or saving them in any 
manner. The unauthorized use, dissemination, distribution, or reproduction of 
this e-mail, including attachments, is prohibited and may be unlawful. If you 
have received this email in error, please notify us immediately by e-mail or 
telephone and delete the e-mail and the attachments (if any).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php