Comments (13)
better to pass a string with the function body :
$filter = 'return $request->method() != Request::POST ? FALSE : $params';
and use create_function:
$this->filters[] = create_function('$route, $params, $request', $filter);
firstly it will avoid problems with the serialization of caching, and secondly less resources.
from core.
It would be the point to store the filters somewhere, e.g. in a module's init.php:
Route::filter('name_of_filter', function($route, $request){ //do some stuff });
And call the filter when needed e.g.
Route::set('default', '(<controller>(/<action>(/<id>)))')
->filter('name_of_filter')
->defaults(array(
'controller' => 'welcome',
'action' => 'index',
));
There's no need to provide the $params variable since you can get that from the request object.
The filters themselves wouldn't need to be serialized, only the name that's stored with the route. There's no need to call create_function. I don't see a single app using dozens of filters. Unless I'm missing what you mean.
Also there's no need to return anything. It would be wiser to Throw a HTTP exception if something went wrong or redirect when needed.
from core.
@happyDemon mistakes:
The filters themselves wouldn't need to be serialized, only the name that's stored with the route.
Routes serialized at caching https://github.com/kohana/core/blob/3.4/develop/classes/Kohana/Route.php#L165
Also there's no need to return anything. It would be wiser to Throw a HTTP exception if something went wrong or redirect when needed.
It's true only if route used single filter, see https://github.com/kohana/core/blob/3.4/develop/classes/Kohana/Route.php#L460
There's no need to provide the $params variable since you can get that from the request object.
this request not contain params yet, also see previous comment
from core.
@WinterSilence routes can be cached, which doesn't mean filters have to be cached with them as well. There's virtually no overhead defining an anonymous function (and remember, the filter doesn't have to be a 'lambda', can be defined as a call to a class method).
One thing I don't like about Laravel's "filters" is that they don't only filter, they do more than that. Maybe there should be a different name for this?
from core.
- Need to split the class code to classes: Router (collection of routes) and Route.
- Need fix: values passed to
Route::defaults()
and 'Route::uri()' are not checked for compliance withRoute::$_regex
- Does it make sense to store routes as objects? Would not it be faster to store data in an array and return the object in
Router::get()
https://gist.github.com/WinterSilence/437395f886177017d4bc my earlier version
routes can be cached, which doesn't mean filters have to be cached with them as well.
Then caching will not make sense. Add example, maybe I did not understand you.
the filter doesn't have to be a 'lambda'
I suggest using a string containing the function body (and use create_function()
) instead of closure, then will not have problems with caching: at serializing closures replaced by the original strings (they stored in special property)
One thing I don't like about Laravel's "filters" is that they don't only filter, they do more than that.
Filters are rarely used, so no need to expand their opportunities.
from core.
Create class wrapper for create_function
: https://gist.github.com/WinterSilence/604e1cd86e242e592795
from core.
Using create_function
is not an acceptable solution for anything.
from core.
@shadowhand Why do you think so?
from core.
@WinterSilence because it's a hack, because you write PHP code in a string and because it's simply not the way Kohana does things. + There is no need to cache methods.
from core.
Seems also that garbage collector doesnt work there.
On May 27, 2014 11:28 PM, "Kemal Delalic" [email protected] wrote:
@WinterSilence https://github.com/WinterSilence because it's a hack,
because you write PHP code in a string and because it's simply not the way
Kohana does things. There is no need to cache methods.—
Reply to this email directly or view it on GitHubhttps://github.com//issues/500#issuecomment-44337947
.
from core.
I'm willing to have a go at this and have a pull request ready in a few weeks.
The main reason I'd like this is because I always seem to be hacking controllers' before method to serve specific needs for several of its actions (when these specific functionality actually should be defined on route level).
from core.
@happyDemon Is the problem you're trying to solve: not being able to use different before/after methods for specific actions and/or specifying which actions should/shouldn't run the before/after methods?
from core.
I used to use my own version of route module that supports filters. I used it only for just a month or so then stopped using it since I've moved on to a new framework.
if you're interested in, checkout https://github.com/moon0326/Kohana-Route-Extended
from core.
Related Issues (20)
- Use of mikey179/vfsStream for Log tests breaks module builds HOT 3
- 3.4.0 current status HOT 49
- Change detecting urls starting with //
- Improvements on website HOT 10
- 1 repo to rule them all HOT 12
- .git files in modules release for 3.3.5 HOT 4
- 4.0.0 release HOT 16
- Ubuntu packages HOT 9
- modules and composer - play together nicer HOT 2
- Should we remove 'action', 'controller', 'directory' from request params? HOT 4
- Implementation of external requests in Minion Task HOT 5
- [Security] Encrypt HOT 34
- Issues with PHP 7.0.6 and ORM HOT 3
- Use Route:url in Minion Task HOT 1
- ERROR: Kohana_Exception [ 0 ]: Directory APPPATH/cache must be writable HOT 8
- "content-length" -header calculation in Response
- Server Upgraded to PHP7 Error: __toString() must not throw an exception HOT 4
- Error en Core Handle
- Function Valid::date for timestamp HOT 1
- [Proposal] Make middlewares HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.