Build a PHP MVC Application: Controllers (Part 5/9)

preview_player
Показать описание

Official site

Twitter
Рекомендации по теме
Комментарии
Автор

well very nice tutorial, nice to follow. but if i am in my root /public/ i get of course a "offset on value of type null" Error because nothing is parsed at the initial visit of my url. i guess we have to check if(isset($url[0]) as well ;)

kratom
Автор

Really enjoying the series of tutorials thus far, hopefully we get calling a form and seeing when the user hits submit where it goes and what happens etc.

MovieHeretic
Автор

if you are getting an error
"Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($function) must be a valid callback, non-static method Foo::bar() cannot be called statically"
downgrade from PHP 8 to PHP 7

luke
Автор

Novice with a tip: Rename the Controller class to 'CustomController' and rename the file that it's in to 'customcontroller' accordingly. Each time you extend 'CustomController' name the new class 'Controller', but put it in a file named accordingly.. so in home.php you'd have a Controller class, and in Contact.php you'd also have a 'Controller' class. Adjust the code to always instance 'Controller'. Because you're only ever including one controller class, there is no name-space clash, and you can avoid case-sensitivity issues in your url.

ChapmanWorldOnTube
Автор

might be easier to extract what you need off the url array using array_shift(). this way you pick off what you need from the beginning and leave the rest as params.

RainnusShin
Автор

Nice video series, explains in a simple and clear way the core techniques.

rommelrodriguez
Автор

Really nice tutorials for Understanding MVC

mdhasanuzzaman
Автор

Hi there, Thanks for the tutorial. It's good to understand.
But at this step i'm running into an issue:

FYI: I'm running PHP 7.0.2 x64 NTS on IIS 8 (Windows Server 2012 R2) with URL Rewrite enabled and was able to get te rewrite conditions work from web.config.
Running the var_dump, echo and print_r tests confirms the controller, method and params are parsed correctly from the url.

So here's my issue:
When I try to make an object instance from $this->controller with the line $this->controller = new $this->controller; i'll get the warning "Creating default object from empty value" and $this->controller stays string "home". When I call the function it returns bool(true), so, the class is recognized.

Is this a PHP 7.0 compatibility problem that might conflict with creating nameless classes?

mastermattie
Автор

Thank you for this tutorial first of all. I got it all working and I'm building my own modules but I'm facing one little important problem. I was wondering how can I pass variables from a form within one of my controllers but to a different method. Ex: I have a controller that takes care of the home class and this class has two methods, Refrigerator and Oven. How can I pass variables using the form within the Refrigerator method to the Oven method?? Currently I have this method "kitchen/refrigerator" with a form submitting to action="kitchen/oven" method="POST" but the variables aren't passing I'm guessing cause there's already a GET method involved within the controller routing index?url=controller?

wolfgangleon
Автор

When I call a method (for example .../public/home/index) I cant see the css, but when I do it like this (.../public/home) It shows the styles. I dont knw why :/

manuelsalas
Автор

if anyone had this problem in the if(file_exist()): "Notice: Trying to access array offset on value of type null"
the solution it's implement isset() function before the if.
if(isset($url[0])){
. $url[0] . ".php")){
$this->controller = $url[0];
unset($url[0]);
}
}

vazquezbandaemiliano
Автор

Doesn't the file exists statement need to be like: . $url[0] . '.php')) not need to be: . $url[0] . 'Controller.php')) instead, as controller names always should end on 'controller' when following MVC naming conventions?

Barrosy
Автор

3:16 why are you doing ../app/controllers instead of ../controllers? since you're in the core folder, you only need to go 1 layer up (you'll be in the app folder) and then go to the controller folder

sweetmelon
Автор

A "ControllerFactory" would fit this example nicely :)

feldinho
Автор

This is an error I got on line 22 of the App.php: Warning: failed to open stream: No such file or directory in on line 22. I don't know where the problem is.

remcodebaas
Автор

the most beautiful thing i have ever seen!!!

jmafoko
Автор

When you setup the routing ... Why did you call a new version $controller.
Wouldn't it be more efficient to use is_callable(), and create the class in the params, this way you don't have to call if method exists.... ?

MichaelDPrice
Автор

Help needed!
Line 26 in app.php generates an error (see video at 3:22).
Code: $this->controller = new $this.controller; This line produces the next error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in on line 26

Thank you in advance for your solution.

edwardvanbakel
Автор

Can i use this route system with PSR-4? i'm saying that because since i'm using namespaces, i wont be able to do that: this->controller = new this->controller; Only if $controller = app\core\Controller

raphaelandrade
Автор

Hey, i know this is an old series. I was just wondering, how would i pass the current controller to the view? For example. Say I have one 'header.php' view, which i just reload every time, as my header doesn't change. But i need to give HTML elements an active class dependant on what controller is loaded. (In my navigation)

Any help would be greatly appreciated

owencyg