简体   繁体   English

如何使用 Sublime Text 显示 PHP 网页(三)?

[英]How to display PHP webpage using Sublime Text (3)?

I have tried following this guide , unfortunately this only displays the code being executed rather than showing how a HTML page with embedded PHP would look like.我曾尝试遵循本指南,不幸的是这仅显示正在执行的代码,而不是显示嵌入 PHP 的 HTML 页面的外观。

What I want is someway to execute index.php file to view how that website would look and behave.我想要的是以某种方式执行index.php文件来查看该网站的外观和行为。 With index.html in sublime text this is easy enough with right click and then Open in Browser .使用 sublime 文本中的index.htmlright click然后Open in Browser就很容易了。 How can I do this with PHP files and Sublime Text 3 or an alternative method with other software?如何使用 PHP 文件和 Sublime Text 3 或其他软件的替代方法执行此操作?

You can use the built-in PHP web server (for PHP >= 5.4.0) which you can run with the following command from the directory where you PHP file is (note that PHP.exe 's directory must be added to the PATH environment variable if you are on Windows)您可以使用内置的PHP Web 服务器(对于 PHP >= 5.4.0),您可以从 PHP 文件所在的目录中使用以下命令运行该服务器(请注意,必须将PHP.exe的目录添加到PATH环境变量(如果您使用的是 Windows)

php -S localhost:8000

A simpler solution would be to install something like XAMPP which has Apache (A web server) already configured with PHP.一个更简单的解决方案是安装XAMPP 之类的东西,它已经用 PHP 配置了 Apache(一个 Web 服务器)。 once installed you can put your PHP files inside the htdocs directory and you'll be able to execute your PHP code by going to http://localhost/yourfile.php .安装后,您可以将 PHP 文件放在htdocs目录中,然后您就可以通过访问http://localhost/yourfile.php来执行 PHP 代码。

I'm answering under the assumption that you already have Apache/PHP running locally.我是在假设您已经在本地运行 Apache/PHP 的情况下回答的。

I think the most popular solution for SublimeText3 is to use the SideBarEnhancements package.我认为 SublimeText3 最流行的解决方案是使用SideBarEnhancements包。 You can use Package Control to install it.您可以使用包控制来安装它。 After you install SideBarEnhancements, you can right click on a PHP file in the sidebar and click "Open In Browser" to see how it would look after being parsed by PHP.安装 SideBarEnhancements 后,您可以右键单击侧栏中的 PHP 文件,然后单击“在浏览器中打开”以查看它被 PHP 解析后的外观。 You might have to right-click on your project and go to Project -> Edit Project URLs to configure the paths to your project.您可能需要右键单击您的项目并转到Project -> Edit Project URLs以配置Project -> Edit Project URLs的路径。 When you select "Edit Project URLs" a new file will open named "SideBarEnhancements.json" where you can configure your paths.当您选择“编辑项目 URL”时,将打开一个名为“SideBarEnhancements.json”的新文件,您可以在其中配置路径。

You should put the following code inside the file.您应该将以下代码放入文件中。

{
    "/path/to/your/document/root":{
        "url_testing":"http://localhost",
        "url_production":"http://www.example.com"
    },
}

To Directly run the PHP web page in browser using Sublime-text you can make a build-system which will run the PHP file in the browser.要使用 Sublime-text 在浏览器中直接运行 PHP 网页,您可以创建一个将在浏览器中运行 PHP 文件的构建系统。 I'm assuming you have apache/nginx installed in your system.我假设您的系统中安装了apache/nginx Make a build file using Tools -> Build System -> New Build System .使用Tools -> Build System -> New Build System创建一个构建文件。
Copy the following code in the file and Save.将以下代码复制到文件中并保存。

    {
    "shell_cmd":"firefox \"localhost/$file_base_name.php\"", 
    "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
    "selector": "embedding.php",
    "variants": 
   } 

Your PHP file should be saved in the Server root folder.您的 PHP 文件应保存在服务器根文件夹中。 Press Ctrl+B to display file in the browser.Ctrl+B在浏览器中显示文件。

You'll need to configure Apache HTTP Server on your local computer.您需要在本地计算机上配置 Apache HTTP Server。 Depending on what type of OS your computer is running, you may already have it - but most people use MAMP or WAMP or some graphic interface to administer it.根据您的计算机运行的操作系统类型,您可能已经拥有它 - 但大多数人使用MAMP或 WAMP 或某些图形界面来管理它。

From there, you'd use a local port (usually localhost:8888) to view the file in your browser.从那里,您将使用本地端口(通常是 localhost:8888)在浏览器中查看文件。

You can use Browser Refresh to save and also refresh the page.您可以使用浏览器刷新来保存和刷新页面。

You can also use something like CodeKit to watch your files and automatically inject the styles or refresh the page.您还可以使用CodeKit 之类的东西来查看您的文件并自动注入样式或刷新页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM