Muestra las diferencias entre dos versiones de la página.
Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
controllers [2016/01/05 19:26] luchothoma |
controllers [2016/12/30 18:42] (actual) |
||
---|---|---|---|
Línea 29: | Línea 29: | ||
URLs can be infinitely long. What that means is that after the controller and function portions on the URL all other portions are just arguments for the controller function. Create a function youSaid() inside about class and remember inside route array set an element 'about/you/:words' => 'about.youSaid') so it looks like this: | URLs can be infinitely long. What that means is that after the controller and function portions on the URL all other portions are just arguments for the controller function. Create a function youSaid() inside about class and remember inside route array set an element 'about/you/:words' => 'about.youSaid') so it looks like this: | ||
- | <code php|about.php> | + | <code php| about.php> |
<?php | <?php | ||
namespace Controller; //Never forget | namespace Controller; //Never forget | ||
Línea 42: | Línea 42: | ||
public function youSaid($message) | public function youSaid($message) | ||
{ | { | ||
- | echo "You said: $message"; | + | echo "You said: ".$message; |
} | } | ||
} | } | ||
Línea 48: | Línea 48: | ||
Now if you visit /about/you/cool it will display "You said: cool". | Now if you visit /about/you/cool it will display "You said: cool". | ||
- | Now there is one problem with this. If you where to visit index.php/about/you then you would get a 404 error! This is because you can't just run youSaid(), it has a required argument! If you want people to still be able to visit the page, then you need to make your argument optional: | + | Now there is one problem with this. If you where to visit /about/you then you would get a 404 error! This is because you can't just run youSaid(), it has a required argument! If you want people to still be able to visit the page, then you need to make your argument optional: |
- | + | <code php> | |
- | public function bob($message = 'nothing at all') | + | public function youSaid($message = 'nothing at all') |
{ | { | ||
- | echo "You said: $message"; | + | echo "You said: ".$message; |
} | } | ||
- | Now visiting /about/you will produce "You said: nothing at all", althoughthis if you visiting /about/bob/cool will still display"You said: cool". | + | </code> |
+ | Now visiting /about/you will produce "You said: nothing at all", althoughthis if you visiting /about/you/cool will still display"You said: cool". | ||
At this point you should try experimenting around with adding additional controllers, functions, and arguments and see what happens. | At this point you should try experimenting around with adding additional controllers, functions, and arguments and see what happens. | ||
Línea 61: | Línea 62: | ||
In Salem is possible to make controller classes extend from other controller classes. For example if you want to extend your about.php controller: | In Salem is possible to make controller classes extend from other controller classes. For example if you want to extend your about.php controller: | ||
+ | <code php| extabout.php> | ||
<?php | <?php | ||
+ | namespace Controller; | ||
+ | use Salem\load;//In this case I use the Load library | ||
- | load::parent_controller('base'); | + | load::parentController('about'); |
- | class another_controller extends base_controller | + | class ExtAbout extends About{ |
- | { | + | function index(){ |
- | public function index() | + | $this->youSaid("I'm cool!"); |
- | { | + | |
- | $this->message(); | + | |
} | } | ||
} | } | ||
- | Parent controllers may have their own parent controllers as well. | + | ?> |
+ | </code> | ||
+ | ---- | ||
+ | [[documentation|Go back to Documentation]] |