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).
Related
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";
I am using PHP header redirect to redirect users from account/?id=$id to user/$username
Successfully it takes for instance account/?id=1 to user/samuel as long as the user exists. Now, this page user/samuel is quite empty ( 404 ).
How do I make it return the user data in the user/samuel using something like isset($_GET[]) manual? in addition of course to adding MYSQL query to retrieve data for the user which has username extracted from the URL, and get their data from database table. and I will be placing all the code in user/index.php
As long as I could make account/?id=$id get the $id from URL ( parameter ) and do other db stuff I think it is also possible to get $username from the URL.. even though user/?username could do it but I don't want to include an ? in the URL..
Any thoughts?
This is a pretty broad topic, what you need to do is parse the url - IE break it into parts and then match the url to a set of actions. This is commonly known as routing.
A naive implementation would be a:
$parts = explode($_SERVER['REQUEST_URI'], '/');
if ( $parts[-2] === 'user' && $parts[-1] ) {
$stmt = $pdo->prepare("SELECT * FROM 'users' WHERE username = ? OR id = ?");
$result = $stmt->execute(array($parts[-1], array($parts[-1]));
// ... do something if the user is found or not.
}
But this would fail if the url contains query parameters (?foo=bar) or a hash (#foo).
Instead you can use parse_url to make sure you only use the path.
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$parts = null;
// Match the url with a regular expression.
preg_match(/^\/user\/(\w*)$/, $path, $parts);
if ( count($parts) == 2 ) {
$stmt = $pdo->prepare("SELECT * FROM 'users' WHERE username = ? OR id = ?");
$result = $stmt->execute(array($parts[-1], array($parts[-1]));
// ... do something if the user is found or not.
}
But in the end you might want consider using a micro framework such as Silex, Slim or using the Routing component from Symfony2 so that you can concentrate on building your application rather than reinventing the wheel.
It might be better if you use Url Rewriting (aka friendly urls)
You can see this link which answers this same question, although your case is a little bit different.
Apache friendly urls
Since you can't convert $id to $username (both are different values) I would recommend to change the link to 'user/ID' instead of 'user/USERNAME'.
$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 ;)
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().
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.