Skip to content

Instantly share code, notes, and snippets.

@automaticalldramatic
Last active December 21, 2015 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save automaticalldramatic/6342752 to your computer and use it in GitHub Desktop.
Save automaticalldramatic/6342752 to your computer and use it in GitHub Desktop.
Laravel - A collection of gists to help me with my laravel setup

Make Laravel(4) work without public/ in the URL

I saw a lot of tricks to modify the .htaccess or to place the other directories outside the root and to simply point the vhost configuration of your apache web server to point to the public directory. None of these make sense when you are developing on an ubuntu box and want to replicate a production environment.

A little bit of snooping around the code will help you competely remove public from the URL. If you have a git repository around the project, you can put this commit in a different branch and cherry-pick it just in case you want to roll back for a future version.

The steps are simple:

  1. Move the contents of the public directory into your application directory.

  2. Modify the public value in bootstrap/paths.php return value to 'public' => __DIR__.'/../../<application-folder>',

  3. Modify the paths in the index.php file that you just moved from the public folder to :

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

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

I don't support the idea of validating data in Controllers against the contrarian argument of doing this using Models. Models are not just merely a map to our data stores but also hold associations, foreign key mappings and other canonical information. Hence, my argument that they should hold validation rules too.

Anyhow, the problem I was facing building a web comic portal was validating a multiple choice selection. I had been working on a clients project to build a social web comics portal. In such a scenario, there exists a many to many relation between the comics added by users and the tags used. For this I had a comics table, a tags table and the customary comic_tag table. The idea was that when users were selecting tags to be added to the comics, there were multiple values passed through a number of checkboxes, all named tags[]. I generally use the static::$rules array set in each model to validate the values submitted using a simple function -

public static function validate($input = null) {
	if (is_null($input)) {
		$input = Input::all();
	}

	return Validator::make($input, static::$rules); 
}

The problem here was that I was submitting an array to the input and I had to check if each value submitted passed three validations:

  1. They were in the tags table
  2. They were integers
  3. And, of course, they were required.

So, I found a nifty little trick to get this done. You iterate through the input array and set rules for each tag type. This is how the modified function looks

public static function validate($input = null) {
	if (is_null($input)) {
		$input = Input::all();
	}
	// We need to do this to not return an error when no tag is selected
	if (array_key_exists('tags', $input)) {
		//Rules for multiple tag selection
		for ($i = 0; $i < count($input['tags']); $i++) {
			static::$rules["tags.{$i}"] = 'required|integer|exists:tags,id';
		}
	} else {
		static::$rules["tags"] = 'required|integer|exists:tags,id';
	}

	return Validator::make($input, static::$rules); 
}

You will have to play with your error messages a little, cause the default messages would be like

Tag.1 is invalid

@automaticalldramatic
Copy link
Author

This is a huge security risk, so until I figure out how to fix this, I reckon I will work this from public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment