How to best implement dynamic url/controller name which is database driven? - php

I have a website that is written in PHP on codeigniter. The scenario I am looking to cope with is the following:
When a user signs up, Their personalised homepage should appear at www.mysite.com/username. Other urls are of the form www.mysite.com/username/pictures & www.mysite.com/username/videos.
So basically what I need is www.mysite.com/username/method (where username is database driven) should always hit the same controller, if the username exists in the database.
Currently, what I am doing is I am using the custom 404 controller to do this. If a URL reaches the custom 404 controller, I check if the controller name matches a username in the data base, then checking the method name and calling the required method. Like so:
$this->load->model('school_model');
$this->load->model('event_model');
$this->load->helper('apps_helper');
$controllerName = $this->uri->segment(CONTROLLER);
$controllerMatch = $this->school_model->findSchoolByUid($controllerName);
$this->data['controller'] = $controllerMatch;
if($controllerMatch != false){
$methodName = $this->uri->segment(METHOD);
if($methodName === "something"){
//Do something
}
if($methodName === "something else"){
//Do something else
}
if($methodName === "another thing"){
//Do another thing
}
if($methodName === "last thing"){
//Do last thing
}
...
}
else{
//Load 404 page
$this->output->set_status_header('404');
$data['content404'] = true;
$this->load->view("common/header", $data);
$this->load->view('frontend/index404', $this->data);
$this->load->view("common/footer");
}
My query is, is this the best way to go about what I need ? How can I improve this ? I have heard of the HMVC modular extension for codeiginiter which may be a good bet ? Just looking for some advice.

You can do this by writing this code in your config > routs.php file
$route['(:any)/pictures'] = 'yourcontrollername/functionName/$1';
now in this if any one write username/pictures url than it will automatically goes to your controller.

Related

Disable span tag in PHP for GLPI

all.
I am trying to disable no-mandatory fields in GLPI. As the application doesn't offer this option, I am trying to change the source code to do it.
This is the piece of code that is related to the mandatory fields:
function getMandatoryMark($field, $force=false) {
if ($force || $this->isMandatoryField($field)) {
return "<span class='red'>*</span>";
}
return '';
}
And this is what I am trying to do:
function getMandatoryMark($field, $force=false) {
if ($force || $this->isMandatoryField($field)) {
return "<span class='red'>*</span>";
}
else{
return "<span onclick='return false;'>*</span>";
}
return '';
}
But when I make this change, create tickets page doesn't load. I am not familiar with PHP so I have no idea what is going on...
No need to edit the files.
Just create a ticket template, name it (e.g. EasyTicket), and make mandatory, add or hide whatever fields you need. I would suggest using Simplified Interface for end users. Less fuss ;)
Manage Templates
Then go to your end users profile (post-only or self-service probably), and choose your Default ticket template (EasyTicket).

CAKEPHP 3.1 clear form after submit

Usually on cakephp2 i used to unset form data and everything was ok.
Some times i use redirects to clear it. But i cant do that in this current page.
Anyone has found this issue or a solution for this?
If you are staying on the "add" page after successfully adding a record, for example to allow entry of multiple records more quickly, you'll need to reset the entity after saving. For example, if you're entering Posts, your controller will look something like:
$post = $this->Posts->newEntity();
if ($this->request->is('post')) {
$post = $this->Posts->patchEntity($post, $this->request->data);
if ($this->Posts->save($post)) {
$post = $this->Posts->newEntity(); // <- Reset the entity
}
}
$this->set(compact('post'));
(Error checking, flash messages, etc. all left out for brevity.)
An alternative is to simply redirect to the same page.
I had the problem that not everything was deleted
$contact = new ContactForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->data)) {
$this->Flash->success(__('Submited.'));
$this->request->data(null);
$contact = new ContactForm();
return $this->redirect('/(Same Page)');// This did the final trick for me
}
}

Codeigniter custom url string

I am new to codeigniter and i am working on making a doctors site and need to allow users to search for doctors using simple url like http://www.mywebsite.com/newyork .so this url doesn't have any class as there can be many states and cant have class for each state. Should i change 404 override in routes script? Or there is a better way to achieve it
$route['(:any)']='search/check_state';
if i use above routing then other page like /login doesnt work.Please help
Make database that contains all states and do it this way
Inside search/check_state
// Get state from URL
$state = $this->uri->segment(1, "");
// Make sure state is not null and its found in database
if($state <> "" && $this->validateState($state)){
// Valid state, continue
}else{
// Give error message / redirect to 404 page
}
Add new function to controller
function validateState($state){
// If database contains $state then return true else return false
}
UPDATE
You can also do it with POST form.
Make form into page where user gives input details
echo form_open(base_url().'path_to_post_validation');
// make form here that sends "state" into page that handles validation
echo form_close();
Handle POST validation
// Make sure user has send POST request
if($this->input->post()){
// Set form validation rules
$this->form_validation->set_rules('state', 'state', 'required|trim|xss_clean');
// Run form validation
if ($this->form_validation->run() == TRUE){
// Store state into variable
$state = $this->input->post('state');
// Check database that it contains $state and then continue or give error message
}
}
got it try
$route['search/check_state/:any'] ='search/check_state';

Zend Framework 1 pass parameters using get to the route

I hope the title does not sound too confusing, but I had no idea how to name my problem.
Brief intro:
I'm using Zend 1.1X.
At the moment I've been working with a search form sending few parameters via POST.
Now I have to change it to use GET, I have a route created looking similar to that:
"search/what/:what/shape/:shape"
and so on, I also have 2 optional parameters which takes null as default.
I'm trying to generate an URL (using Zend View Helper Url) at form's action, but it throws an exception:
Uncaught exception 'Zend_Controller_Router_Exception' with message what is not specified
I Now don't have idea what should I do. If I change my route to "search" only, it then sends the form correctly, but I end up with "search?what=XXXX&shape=YYYY" instead of "search/what/XXXX/shape/YYYY".
Is there any way that could be handled the way I like??? :>
#EDIT
I think this should also be mentioned - I have a different form, similar one, pointing to a route without parameters specified as well and the uri gets "translated" to the form of "key/value" pairs. The only difference between them is that the first one does not use Url helper, instead has the method part hard-coded and my form is being submitted programatically (button => jQuery stuff => submit). Would that make a difference here, as I believe it should not? :>
I hope any possible source of this behaviour will come up to you, because I'm really stuck at the moment and I simply can't find what's wrong..
Thanks in advance!
With the GET method a form generates an url like this: action?param1=val1&param2=val2&....
I see two solutions:
The first is to regenerate the URL by javacsript, we can imagine something like this:
<form method="get" id="id_form">
....
</form>
<script>
var objet_form = document.getElementById('id_form');
function gestionclic(event){
var url = objet_form.action;
for(var i = 0; i < objet_form.length; i++){
url += "/" + objet_form[i].name + "/" + objet_form[i].value;
}
objet_form.action = url;
}
if (objet_form.addEventListener){
objet_form.addEventListener("submit", gestionclic, false);
} else{
objet_form.attachEvent("onsubmit", gestionclic, false);
}
</script>
But I don't think this is a good solution.
The second is to manage it with a plugin:
For the plugin, it must be declared in the bootstrap.
For example:
public function _initPlugins(){
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new Application_Plugin_PRoutage());
}
with this example, the application/plugins folder, create the PRoutage.php plugin like this:
class Application_Plugin_PRoutage extends Zend_Controller_Plugin_Abstract
{
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
...
}
}
and with the variable $request you have access to your data as an array with $request->getParams().
We can imagine something like this:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$param = $request->getParams();
$what = "";
$shape = "";
if (isset($param['what']) $what = $param['what'];
if (isset($param['shape']) $shape = $param['shape'];
if ($what == "XXXX" && $shape == "YYYY"){
$request->setControllerName('other_controler')
->setActionName('other_action')
->setDispatched(true) ;
}
}
I hope it will help you

Codeigniter if controller

First of all sorry if its a noob question.
But is it posibble to do this in codeingiter, like if i have a sidebar but i only want to load it in 2 pages
if(controller == 'blog') {
//load sidebar
}
just like in wordpress if is_page
Use $this->router->fetch_class()
if($this->router->fetch_class() == 'blog') {
//load sidebar
}
Also $this->uri->segment(2) will work in most cases, but in some cases like mod_rewrite or when using subfolder or route it may fail.
More simply you can do like this.
$controller_name = $this->CI->router->fetch_class();
if($controller_name === "your_controller_name")
{
//your logic
}

Categories