CodeIgniter: Dicas

Hoje eu vou dar alguams dicas que eu uso em todo projeto envolvendo CI. Primeiro, no “url_helper“, eu fiz a seguinte modificação na função base_url():

[cc lang=”php”]
if ( ! function_exists(‘base_url’))
{
function base_url($link=FALSE)
{
$CI =& get_instance();
$baseUrl = $CI->config->slash_item(‘base_url’);
if ($link===TRUE) $baseUrl .= $CI->config->slash_item(‘index_page’);
return $baseUrl;
}
}
[/cc]

Isso me permite usar a função em links, onde eu não sei se o host final suporta mod_rewrite. Assim, eu posso usar

[cc lang=”php”]

[/cc]

que ele automaticamente coloca o index.php antes dos controllers dependendo do seu config.php, e em caso de imagens e inserts de .js, ou .css, é só usar normalmente, base_url(). Normalmente não mecho com o Core do CodeIgniter, portanto eu salvei uma copia do url_helper modificado na minha pasta helpers dentro da pasta application.

Segundo. Essa é muito simples. Quando eu uso a classe Form Validation, e eu tenho um campo em branco que não é obrigatório, e obviamente eu nao precisaria usar uma função set_rules, o campo não é preenchido. No entanto, se eu usar uma função set_rules sem especificar uma regra,

[cc lang=”php”]
$this->form_validation->set_rules(‘field_name’, ”, ”);
[/cc]

eu consigo deixar o campo em branco no formulário, mas se eu digitar alguma coisa, o campo ainda assim é repopulado.

E por último, quando eu uso a classe de upload, as vezes eu tenho que apagar o arquivo quando eu edito um registro com uma foto, por exemplo. Entao eu preciso do caminho absoluto da raiz da aplicação. Não achei nenhuma constante definida pra este caso, então criei uma. No arquivo index.php, insira esta linha após a ultima declaração de constantes:

[cc lang=”php”]
define(‘FULLPATH’, pathinfo(__FILE__, PATHINFO_DIRNAME));
[/cc]

Agora, eu tenho a constante FULLPATH que guarda o caminho absoluto da minha aplocação. Espero que essas pequenas dicas tenham sido útil para alguem 😉

Did you like this? Share it:

25 Comments

  • Natanael |

    Muito boa dica Vicente, estou começando com CI (só fiz 4 projetos até agora), mas estou gostando muito, o bom de tudo é que tem uma grande comunidade que conhece e usa o CI, fica facil encontrar dicas como essa que ajuda e enriquece esse podereso Frameworks

  • Bernardo |

    Vicente, dica muito boa, poderia criar mais artigos assim.

    Não sou experte em PHP e estou trabalhando com CI. Essa definiçãod e constante resovel meu problema. Agora no Windows ou Linux não tenho problema.

    define(‘FULLPATH’, pathinfo(__FILE__, PATHINFO_DIRNAME));

  • Blair |

    I’ve read several excellent stuff here. Definitely value bookmarking for revisiting.
    I wonder how much attempt you set to create the sort of magnificent
    informative site.

  • Eve |

    I think what you posted was very reasonable.
    But, think about this, what if you typed a catchier title?
    I am not suggesting your information isn’t solid, but suppose you added a headline that makes
    people want more? I mean CodeIgniter: Dicas | Vicente Russo is kinda vanilla.

    You ought to look at Yahoo’s front page and
    see how they write post titles to grab people interested.

    You might add a video or a related picture or two to get people excited about everything’ve got to say.
    In my opinion, it might bring your website a little bit more interesting.

  • hi |

    Appreciating the time and effort you put into your website and in depth information you offer.
    It’s great to come across a blog every once in a
    while that isn’t the same out of date rehashed material.
    Great read! I’ve bookmarked your site and I’m including your RSS feeds
    to my Google account.

  • are coconut oil |

    I think what you published made a ton of sense. However, what
    about this? suppose you wrote a catchier title? I mean, I don’t
    want to tell you how to run your website, but suppose you added a title that makes
    people desire more? I mean CodeIgniter: Dicas | Vicente
    Russo is a little vanilla. You might peek at Yahoo’s
    front page and note how they create post titles to get viewers
    to open the links. You might try adding a video or a pic or two to get people excited about what
    you’ve written. In my opinion, it would make your website a little bit more interesting.

  • nike air force |

    There are some interesting cut-off dates in this article however I don抰 know if I see all of them middle to heart. There’s some validity but I will take hold opinion till I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as properly

  • tinyurl.com |

    Thanks for one’s marvelous posting! I really enjoyed reading it, you happen to be a great author.
    I will be sure to bookmark your blog and may
    come back very soon. I want to encourage yourself to continue your great writing, have a nice evening!

  • tinyurl.com |

    Howdy would you mind letting me know which web host you’re working with?
    I’ve loaded your blog in 3 different internet browsers and I must say this blog
    loads a lot faster then most. Can you suggest a good internet hosting provider at a
    honest price? Many thanks, I appreciate it!

  • air max 90 |

    That is the fitting blog for anybody who wants to seek out out about this topic. You realize so much its nearly hard to argue with you (not that I really would need匟aHa). You positively put a new spin on a topic thats been written about for years. Great stuff, just great!

  • coconut oil and |

    Hello There. I discovered your blog the use of msn. That is
    a really well written article. I’ll be sure to bookmark it and come back to read more of your helpful
    information. Thanks for the post. I’ll certainly
    return.

  • balenciaga shoes |

    Youre so cool! I dont suppose Ive read something like this before. So nice to search out any individual with some unique ideas on this subject. realy thanks for beginning this up. this web site is one thing that’s needed on the net, somebody with slightly originality. useful job for bringing one thing new to the web!

  • ps4 games |

    Hello there! Quick question that’s completely off topic. Do you know how to make your site mobile friendly?
    My site looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might be able to fix this problem.
    If you have any recommendations, please
    share. Thanks!

  • quest bars cheap |

    Thanks for your personal marvelous posting! I genuinely enjoyed reading it,
    you could be a great author. I will be sure to bookmark your blog and may
    come back at some point. I want to encourage you to definitely
    continue your great posts, have a nice day!

  • quest bars cheap |

    Excellent post. I was checking continuously this weblog and I’m inspired!

    Extremely useful information specifically the closing phase 🙂 I maintain such info a lot.
    I used to be seeking this certain info for a long time.
    Thanks and best of luck.

  • ps4 games |

    Way cool! Some very valid points! I appreciate you writing this write-up and also the rest
    of the site is extremely good.

  • ps4 games |

    When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three e-mails with
    the same comment. Is there any way you can remove people from that service?
    Many thanks!

So, what do you think ?