Welcome Guest, Not a member yet? Register   Sign In
Building a nav upon different controller's methods.
#1

[eluser]Bramme[/eluser]
Hello everybody

I'm working on a rather large app that will have a back end with multiple controllers and several usergroups where restrictions apply.

At the moment, I'm putting all of my controllers functions in one config file and build the navigation for the nav from that config array. I also (manually) add unique id's in the array, so methods can be easily identified by the restriction function in the auth library.

However, this is a real hassle.

I was wondering if I could automate this process and write a separate method that would go through all my controllers, look up the methods, give them a unique id and combine it in an array.

I'm pretty sure this is possible, however, I've got no idea of how to begin. Anyone know some php functions I could use? General tips on programming logic are also welcome!
#2

[eluser]Dam1an[/eluser]
A combination of opendir and readdir to get the controllers and get-class_methods() should get you heading in the right direction
And just use a standard increment for the IDs
#3

[eluser]Bramme[/eluser]
My main concern is though: will the same id's be appointed the the same methods every time? I guess if I don't rename any controllers/methods I don't have to fear that.

Another major issue is: How can I use pretty names for my navigation text? Would be kinda silly tot have a navigation that read "users -> add_user".

But then again, I guess I could name my methods consistently like this and then replace the _ with a space and capitalize the first word.
#4

[eluser]Dam1an[/eluser]
Ah, there's a point, if you add a function to the first controller it will make everything else off by one Sad
If the ID doesnt need to be numeric, you could always just do a hash of controller.function

And yeah, I would give make sure function names as as you'd want them to appear and just replace underscores with spaces and capitalize... anything more then that, and you'd have to set it yourself, which kind of defeats the point
#5

[eluser]Bramme[/eluser]
hash of controller.function sounds great actually and would solve the problems. in my case, it doesn't even need to be a hash I think.

What I just thought off: my views are sorted in a controller/method.php way. Each view starts with a title tag. I might as well just grab the text from the view file.
#6

[eluser]renownedmedia[/eluser]
Maybe cache the results in a db or flat text file, and have a hidden URL you can visit to rebuild the cache, that way you're not checking every pageview.

You could comment the controllers with some sort of syntax like this:

Code:
function add_user() {
/*
* Nav-Display: 'Add User'
* Nav-Display-User-Level: 5+ (some sort of permission system for which can be displayed)
* Nav-Url: inherit (could be an override on the url, like 'add_user/whatever'
*/
do_stuff();
}

And when parsing your files to build a navigation, you wouldn't need to do a get class methods, but instead just parse the PHP files and read their contents.
#7

[eluser]Bramme[/eluser]
actually, that's quite brilliant! thanks for the hint, Thomas, gonna try and work that out!
#8

[eluser]Bramme[/eluser]
Okay, I've implemented a comment in all methods that need to be in the navigation like this:

//*users|Users|manage_usergroups|Manage usergroups*//

With glob() I'm going through my controllers and with the help of RegExr, I managed to build an expression that matches my comments.

But now I'm a little stuck.

What is the best way to use the regular expression? At the moment I'm using the preg_replace_callback function, but it feels like I'm overcomplicating things Confused


Edit: after typing this, I had a look at the php documentation and realized I should use preg_match_all... Nevermind :p


Edit2: okay, after having managed to build the array, I'm now stuck as to how I should cache it.




Theme © iAndrew 2016 - Forum software by © MyBB