Need regex to match routes in Codeigniter - php

I am having routes configuration like below,
$route['sf_network/list'] = "sf_resource/list";
$route['sf_client/list'] = "sf_resource/list";
$route['sf_subnet/list'] = "sf_resource/list";
$route['sf_ip_address/list'] = "sf_resource/list";
$route['sf_network/describe/(:any)'] = "sf_resource/describe";
$route['sf_client/describe/(:any)'] = "sf_resource/describe";
$route['sf_subnet/describe/(:any)'] = "sf_resource/describe";
$route['sf_ip_address/describe/(:any)'] = "sf_resource/describe";
Similar to this I am also need to configure routes for add and modify pages. so, its becoming too large lines of coding. Is there anyway to use regex for reduce the multiple lines of coding. I am new to PHP and this is my first project. Any suggestion would be most grateful. Thank you.
Solution
$route['sf_([A-Za-z_-]+)/list'] = "sf_resource/list";
$route['sf_([A-Za-z_-]+)/describe/(:any)'] = "sf_resource/describe/$2";

Related

Fortnite API html

I want to put my fortnite stats on my website HTML/CSS. I find this.
But I don't know php very good, so I need your help. Must I delete this: 'your_api_key' and put : your_api_key without the ' ' ?
And lines like this:
$this->auth = new Fortnite_Auth($this);
$this->challenges = new Fortnite_Challenges($this);
$this->leaderboard = new Fortnite_Leaderboard($this);
$this->items = new Fortnite_Items($this);
$this->news = new Fortnite_News($this);
$this->patchnotes = new Fortnite_PatchNotes($this);
$this->pve = new Fortnite_PVE($this);
$this->status = new Fortnite_Status($this);
$this->weapons = new Fortnite_Weapons($this);
$this->user = new Fortnite_User($this)
Must I modify something?
(here are some informations:
-user_id: 501b9f2dfda34249a2749467513172bf
-username: NoMaD_DEEPonion
-platform: pc
-windows: season 5
)
For all this code, I used xammp server (I hope it's good)
Thank you for your help
You should always quote '' your key/strings. So it would look like:
$api->setKey('123qwerty456');
$api->user->id('NoMaD_DEEPonion');
Please read their documentation. You're supposed to get an API key from them to get started. And you don't have to edit Client.php. You're supposed to include the files instead. Try including Autoloader.php, since it requires the necessary files.
XAMPP is alright for development, but not suitable for production/public. Good luck!
What #sykez wrote.
I personally use https://fortniteapi.com/ to get the data. They also have a sweet POSTMAN page with different requests https://documenter.getpostman.com/view/4499368/RWEjoGqD
At last. I am not really sure that this will be a good project to start learning PHP from. I really suggest that you get to understand the basics of PHP first before you jump into API calls, processing arrays and more.

Codeigniter routing segments/params

$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
in this case, I will have to link segment1/someMethod/someParam, but what if I don't have params in the url, i will need to write this:
$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
thus the both cases will now work, so the question is: Can I write those 2 lines of code in one shot?
this:
$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
is duplicating,they do the same thing, use just one of them, i suggest
$route['segment1/(:any)'] = "myController/$1";
hope to be clear, when using this "myController/$1" you are saying everything following myController/ should be routed, and it works also if no $1 params exists.
Definitely use just one of them and don't scare about not having params, it works like a charm ;)

How to find and replace a line in multiple files with PHP

I want to create a installer for my current project, that automatically modifies a dozens of config files.
So if the form was sent, the PHP script should look in which config file the searched option is and change it. Before you ask, I cant put the files together ;) .
A basic config line looks like this:
$config['base_url'] = 'test';.
I tried to use str_replace()but this didn't work because I don't know what is currently in the variable.
So I need a function that searches for $config['base_url'] = '%'; in multiple files and replaces it with $config['base_url'] = 'new_value'; (for example).
I realise the answer's accepted, and originally I deleted this, however, in the comments you mention the config being editable, which presumably means by other users, so you can't guarantee the spacing will match, nor that they'll use ' instead of " always, so the following is perhaps a little more forgiving
$name = 'base_url';
$value = 'new_value';
$config = '$config["base_url"] = "old_value";';
$config = preg_replace('/\[(?:\'|\")'.$name.'(?:\'|\")\]\s*=\s*(\'|\")(.*)\\1;/', "['".$name."'] = '$value';", $config);
echo '<pre>', var_dump($config), '</pre>';
You can use a regular expression like the following:
/\$config\['base_url'\] = '[a-zA-Z0-9]';/
Which you would have to adapt to each line.
A better solution, in my opinion, would be to create a template config file with lines like the following:
$config['base_url'] = '%BASE_URL%';
Where you could simply use str_replace().

Zend Framework : Need All parameters for application.ini

I want to know all the parameters/options which can be used in application.ini file. Is there any list available?
Please help
You can specify anything you want. For instance, I set my javascript files there:
js.jquery.filename = "/js/jquery.min.js"
js.jquery.offset = 0
js.jqueryui.filename = "/js/jquery-ui.min.js"
js.jqueryui.offset = 1
js.nestedsortable.filename = '/js/jquery.ui.nestedSortable.js'
js.nestedsortable.offset = 2
js.ckeditor.filename = "/js/ckeditor/ckeditor.js"
js.ckeditor.offset = 3
Now, whenever I need to add a javascript file, I do:
$config = Zend_Registry::get('config');
$js = $config['js'];
$this->view->headScript()->offsetSetFile($js['ckeditor']['offset'],$js['ckeditor']['filename']);
$this->view->headScript()->offsetSetFile($js['jquery']['offset'],$js['jquery']['filename']);
Like I said, you can specify any value. Anything you will be accessing often and need to be available globally can and perhaps should be there :).
During loading config in index.php you can store it in Zend_Registry and you can access them when you want.
You can also parse Zend_Config to an array and look, what is inside.
#Future King Do this:
$config = Zend_Registry::get('config');
Zend_Debug::dump($config);
That should do it. Let me know if it helps.

Zend Framework routes not being applied

I have the following routes in my application.ini:
resources.router.routes.user.route = "users/:id/*"
resources.router.routes.user.defaults.controller = users
resources.router.routes.user.defaults.action = profile
resources.router.routes.user.reqs.id = "\d+"
resources.router.routes.page.route = "pages/:date/*"
resources.router.routes.page.defaults.controller = pages
resources.router.routes.page.defaults.action = index
resources.router.routes.page.reqs.date = "\s+"
resources.router.routes.write.route = "pages/write/:type/*"
resources.router.routes.write.defaults.controller = pages
resources.router.routes.write.defaults.action = write
resources.router.routes.write.reqs.type = "\s+"
However, only the first is applied. ZF attempts to use the parameter as an action, so from what I can tell the route is simply ignored. What am I doing wrong that stops the other routes from working?
Figured it out. Routes were being ignored as I was using
resources.router.routes.write.reqs.type = "\s+"
I'm not even sure there is a \s, but used it out of guesswork (not sure why it worked before though).

Categories