In this article, I will show you how to host a site built with Laravel on a shared host without SSH and also how to upload files the wrong way but it surely works.

If you are a fan of WWE, you will do well to remember that notorious disclaimer in between fights: ‘Don’t try this at home’. I am just saying that this is not the best way to host a site built with Laravel because it takes away many flavors that comes with Laravel but if you are running on tight budget or curious about doing things the wrong way. Then this is for you.

I will assume you already set up a laravel project, if you have not, visit Laravel docs.

Setup your hosting plan with everything that comes with it. Assuming our project is named CstdWeek and in a folder with the same name. We are going to transfer our project folder (yes, even the vendor folder but make sure you have checked and compiled everything worth compiling) to the shared server. Our project folder in this case should be CstdWeek, it should be on the same level as the public_html folder.

host filemanager

I tried compressing the project folder into a zip file and transferring the zip file but I was always hitting an error because of the javascript file inside the zip folder. Transfer the folder through FTP, thats the best way.

Delete the content of your public_html folder (not the folder itself) and move the contents of your project’s public folder (also create a new folder docs which we will use for file upload) to the empty public_html.

project's public folder
project’s public folder after moving the contents and creating docs folder.
public_html folder
public_html folder after moving the contents of the public folder.

Edit these lines in the index.php file, from:

 

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

To:

require __DIR__.'/../CstdWeek/vendor/autoload.php';

$app = require_once __DIR__.'/../CstdWeek/bootstrap/app.php';

 

You can use the full file path if this didn’t work for you.

That is it, your Laravel Project is ready to go.

Your project may need the file upload functionality and like I said, hosting a laravel site like this takes away many flavors from it. The right way is to create a symbolic link between the public/storage folder and the Storage/app folder but we will like to do it the wrong way.

Lets start by creating somes routes first in our routes/web.php file:

Route::get('/uploadpaper', 'UploadController@index')->name('uploadpaper'); //opens the upload form page

Route::post('/upload', 'UploadController@upload'); //uploads the file to the right folder

 

Next, we create our upload form:

<form class="form-horizontal" method="POST" action="{{ url('/upload') }}" enctype="multipart/form-data" >
	{{ csrf_field() }}
	<div class="form-group{{ $errors->has('doc') ? ' has-error' : '' }}">
		<label for="doc" class="col-md-4 control-label">Choose Paper</label>
		<div class="col-md-6">
			<input id="doc" class="form-control" type="file" name="doc" required autofocus>
			@if ($errors->has('doc'))
				<span class="help-block">
					<strong>{{ $errors->first('doc') }}</strong>
				</span>
			@endif
		</div>
	</div>

	<div class="form-group">
		<div class="col-md-6 col-md-offset-4">
			<button type="submit" class="btn btn-primary">Upload</button>			</div>
	</div>

</form>

 

Create a controller with the appropriate methods: php artisan make:controller UploadController

And add this method to it:

public function upload(Request $request) {
	$paper = new Paper;
	$doc = $request->input('doc');

	validator([
		$doc  => 'required|mimes:docx,doc,pdf,ppt,pptx|max:2048',
		]);
//rename image
	$docName = substr($email, 0, 3).rand(1000, 999999).'.'.$request->doc->	getClientOriginalExtension();

	$request->doc->move('/home/cstdspac/www/docs', $docName); //move the file to the 	right folder

	$paper->save();
	return back()->with('status', 'Paper upload is successful');

	}

 

Observe what we did here, we had to move the file into the public_html/docs folder and not into the public/docs folder or Storage/app folder (which is the right way). This way the file can be accessed through the web.

I know this might sound somehow but actually it was very exciting.

You can drop your comments or if you know of a better way to follow this wrong way, feel free to share.

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here