Laravel 4 removing public from URL

后端 未结 20 1980
一个人的身影
一个人的身影 2020-11-29 04:33

So, I\'m running xampp on Windows. I\'m currently trying to get familiar with the laravel framework. Now, when thats pointed out. How can i be able to access my laravel appl

相关标签:
20条回答
  • 2020-11-29 04:41

    Set you document root for apache to the public folder, and not the laravel folder. This is the simplest technique and recommended for production environments.

    0 讨论(0)
  • 2020-11-29 04:42

    Here's how I did it.

    1. Edit your Windows Host file - C:\Windows\System32\drivers\etc\hosts
    2. Edit the Apache vhosts file - Drive-Letter:\xampp\apache\conf\extra\httpd-vhosts.conf
    3. Add an htaccess file to the laravel/public folder (if its not already there)
    4. Restart Xampp apache server

    Windows can be a real PITA when trying to edit the Hosts file because of the User Account Control. Since I work on all kinds of small hobby projects, I have to edit this file all the time so this is what I do.

    • Install PSPad. It loads really fast and you can bookmark files for easy loading/editing. Sublime Text also works well if you load the two files I mentioned above and save the workspace as a new project.
    • Right-click on the PSPad (or other editor) program shortcut and choose 'Run as Administrator'. You cannot save changes to the Hosts file unless you do this.
    • Open the Windows Host file in the editor. This file does not have a file extension, so you have to choose "All Files" in the File Open dialog to even see the file.
    • At the bottom of the file, add this:

      127.0.0.1  laravel.dev
      

      This tells Windows to point the web browser to localhost whenever you enter laravel.dev in the browser's address bar.

    • Save the file.
    • Open the xampp Apache httpd-vhosts.conf file.
    • At the bottom of the file, add this: (I am assuming xampp is installed at the root of the D: drive)

      <VirtualHost *:80>
        ServerName laravel.dev
        DocumentRoot "D:/xampp/htdocs/laravel/public"
        <Directory "D:/xampp/htdocs/laravel/public">
        </Directory>
      </VirtualHost>
      
    • Add an htaccess file to your laravel/public folder (if its not already there). I think the default htaccess file that comes with L4 looks like this:

      Options -MultiViews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^ index.php [L]
      
    • Restart your xampp apache server.
    • Open a web browser and type in the address bar - http://laravel.dev
    • That will take you to the index.php file in the "public" folder.
    • To get to the About page, I think the address would be http://laravel.dev/about
    0 讨论(0)
  • 2020-11-29 04:43

    BEST Approch: I will not recommend removing public, instead on local computer create a virtual host point to public directory and on remote hosting change public to public_html and point your domain to this directory. Reason, your whole laravel code will be secure because its one level down to your public directory :)

    METHOD 1: I just rename server.php to index.php and it works

    METHOD 2:

    Here is my Directory Structure,

    /laravel/
    ... app
    ... bootstrap
    ... public
    ... etc
    

    Follow these easy steps

    1. move all files from public directory to root /laravel/
    2. now, no need of public directory, so optionally you can remove it now
    3. now open index.php and make following replacements

    require DIR.'/../bootstrap/autoload.php';

    to

    require DIR.'/bootstrap/autoload.php';

    and

    $app = require_once DIR.'/../bootstrap/start.php';

    to

    $app = require_once DIR.'/bootstrap/start.php';

    1. now open bootstrap/paths.php and change public directory path:

    'public' => DIR.'/../public',

    to

    'public' => DIR.'/..',

    and that's it, now try http:// localhost/laravel/

    0 讨论(0)
  • 2020-11-29 04:43

    I have found geart flow to work with laravel localy.

    What you can do is to configure xampp a bit. At your xamp's httpd.conf file you have to find document DocumentRoot and <Directory>. Change root directory to yours laravel public folder and restart apache. Since when you can access your project simplly just typing localhost. Now if you want you can change your host file and rewrite local dns, for example: 127.0.0.1 example.laravel.com and now you can access your project with real url. It may look bit complicated, but it's not.

    Alternative to that would be php artisan serve. You can start server on different ports and when re-write hosts file.

    You could add some features to improve your workflow even more, for example vagrant or ngrok. You can share your project for live presentation (speed may be issue here).

    0 讨论(0)
  • 2020-11-29 04:44

    rename the server.php to index.php and copy .htaccess from /public is the right way. If you send your app online,just change DocumentRoot to the path of public.

    0 讨论(0)
  • 2020-11-29 04:46

    Need to remove public segment in the larvel4 app

    Laravel 4 requires you to put your app code one level higher than the web root, and this causes problems for some developers that are stuck on shared hosting and that doesn’t allow a setup like this. It’s actually really easy to get around it. I read that some L4 specific packages could have problems on a setup like this, but I didn’t experience anything like that with any package yet.

    So first install L4 somewhere you like. I liked the article Niall wrote on keeping the base L4 app up to date, so go and check that out: Installing and Updating Laravel 4

    I find it’s enough for this example to simply clone the repo (assuming you have composer installed globally, if not, go to http://getcomposer.org/):

    git clone -b develop git://github.com/laravel/laravel.git app_name
    

    php composer install

    Note that we are cloning the develop branch since L4 is still in beta at this time.

    So to remove the “public” part from your URL, simply move all files and folders from public to your app root and you’ll end up with a folder structure like this:

    /app
    /bootstrap
    /packages (copied from /public)
    /vendor
    .htaccess (copied from /public)
    artisan
    composer.json
    favicon.ico (copied from /public)
    index.php (copied from /public)
    robots.txt (copied from /public)
    server.php
    

    Now we need to edit our paths in index.php:

    require __DIR__.'/bootstrap/autoload.php';
    
    $app = require_once __DIR__.'/bootstrap/start.php';
    

    And then just set the public dir in out /bootstrap/paths.php file:

    'public' => __DIR__.'/..',
    

    this is my suggession

    0 讨论(0)
提交回复
热议问题