Pergunta

I am using MAMP (just and example) to develop web applications without having to constantly upload and download via FTP.

I´m sure this is the correct way to test a project but I´m no sure if I´m doing it right.

After I´m satisfied with the project I have to change in every page the MySQL databases and most of the links etc.

Is there a workaround this? How should this be used?

Thanks in advance!

Foi útil?

Solução

Whether it is local or not, you need a development environment outside of production.

I'm no PHP expert, but every page should not have its own connection code to the database. Create a class that can be reused. Your application should be able to determine if you are on local host or example.com and connect to the appropriate database.

And for hyperlinks, look into Relative URL instead of Absolute URL.

Outras dicas

It's always better to have a common class which contains your database connect, database disconnect and your most used paths like

$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'];
$config['base_path']= $_SERVER['DOCUMENT_ROOT'].'/project/';

These are very help full.

Instead of hard coding the links, use a variable to hold the base URL, then append that as a prefix.

I'd have a configuration file with both the database connection info AND the server URL, you could actually use

define('SERVER_URL', 'http://' . $_SERVER['SERVER_NAME'] . '/');

It's what I use when I work with WordPress :)

I Would have a configuration file, which contains the bits that you have to change every time, so that you will only have to make a few changes.

Yet another way, in the very unlikely case that none of the above suggestions works, is to put a line in your hosts file (/etc/hosts on Unix or \windows\system32\drivers\etc\hosts on Windows) such as:


127.0.0.100 my.server.com  # or any number in the 127/8 network.

Just don't forget to remove it, or comment it out with a preceding "#", when you actually want to connect to my.server.com. It can be very confusing, annoying, and even dangerous if you forget. "Oh no, how did all my private data end up here on the server??? rm -rf private/. oops..."

Licenciado em: CC-BY-SA com atribuição
scroll top