Skip to content

Commit

Permalink
Merge pull request aaronlord#15 from xeno010/master
Browse files Browse the repository at this point in the history
Updated to Laravel 5. @xeno010 <3
  • Loading branch information
aaronlord committed Feb 13, 2015
2 parents 31fc8e8 + 513590b commit ddcaaa7
Show file tree
Hide file tree
Showing 20 changed files with 332 additions and 189 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
composer.phar
composer.lock
.DS_Store
.idea
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
],
"license" : "MIT",
"require": {
"php" : ">=5.3.0",
"illuminate/support" : "~4"
"php" : ">=5.4.0",
"illuminate/support" : "5.0.*",
"illuminate/routing": "5.0.*",
"illuminate/console": "5.0.*",
"illuminate/config": "5.0.*",
"illuminate/filesystem": "5.0.*"
},
"require-dev" : {
"phpunit/phpunit" : "3.7.*",
"phpunit/phpunit" : "~4.0",
"mockery/mockery" : "dev-master"
},
"autoload": {
"psr-0": {
"Lord\\Laroute": "src/"
"psr-4": {
"Lord\\Laroute\\": "src/"
}
},
"minimum-stability": "stable"
"minimum-stability": "dev"
}
52 changes: 52 additions & 0 deletions config/laroute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

return [

/*
* The destination path for the javascript file.
*/
'path' => 'public/js',

/*
* The destination filename for the javascript file.
*/
'filename' => 'laroute',

/*
* The namespace for the helper functions. By default this will bind them to
* `window.laroute`.
*/
'namespace' => 'laroute',

/*
* Generate absolute URLs
*
* Set the Application URL in config/app.php
*/
'absolute' => false,

/*
* The Filter Methode
*
* 'all' => All routes except "'laroute' => false"
* 'only' => Only "'laroute' => true" routes
* 'force' => All routes, ignored "laroute" route parameter
*/
'filter' => 'all',

/*
* Controller Namespace
*
* Set here your controller namespace (see RouteServiceProvider -> $namespace) for cleaner action calls
* e.g. 'App\Http\Controllers'
*/
'action_namespace' => '',

/*
* The path to the template `laroute.js` file. This is the file that contains
* the ported helper Laravel url/route functions and the route data to go
* with them.
*/
'template' => 'vendor/lord/laroute/src/templates/laroute.js',

];
68 changes: 55 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Laroute
# Laroute for Laravel 5

### For Laravel 4.x, check [version 1.3.2](https://github.com/aaronlord/laroute/tree/v1.3.2)

[Laravel](http://laravel.com/) has some pretty sweet [helper functions](http://laravel.com/docs/helpers#urls) for generating urls/links and its auto-json-magic makes it building APIs super easy. It's my go-to choice for building single-page js apps, but routing can quickly become a bit of a pain.

Expand All @@ -16,7 +18,7 @@ Install the usual [composer](https://getcomposer.org/) way.
```json
{
"require" : {
"lord/laroute" : "1.*"
"lord/laroute" : "2.*"
}
}
```
Expand All @@ -38,48 +40,73 @@ Install the usual [composer](https://getcomposer.org/) way.
Copy the packages config files.

```
php artisan config:publish lord/laroute
php artisan vendor:publish
```

###### app/config/packages/lord/laroute/config.php

```php
<?php

return array(
return [

/**
/*
* The destination path for the javascript file.
*/
'path' => 'public/js',

/**
/*
* The destination filename for the javascript file.
*/
'filename' => 'laroute',

/**
/*
* The namespace for the helper functions. By default this will bind them to
* `window.laroute`.
*/
'namespace' => 'laroute',

/**
/*
* Generate absolute URLs
*
* Set the Application URL in config/app.php
*/
'absolute' => false,

/*
* The Filter Methode
*
* 'all' => All routes except "'laroute' => false"
* 'only' => Only "'laroute' => true" routes
* 'force' => All routes, ignored "laroute" route parameter
*/
'filter' => 'all',

/*
* Action Namespace
*
* Set here your controller namespace (see RouteServiceProvider -> $namespace) for cleaner action calls
* e.g. 'App\Http\Controllers'
*/
'action_namespace' => '',

/*
* The path to the template `laroute.js` file. This is the file that contains
* the ported helper Laravel url/route functions and the route data to go
* with them.
*/
'template' => 'vendor/lord/laroute/src/Lord/Laroute/templates/laroute.min.js',
'template' => 'vendor/lord/laroute/src/templates/laroute.js',

];

);
```

### Generate the `laroute.js`

To access the routes, we need to "port" them over to a JavaScript file:

```
php artisan generate:laroute
php artisan laroute:generate
```

With the default configuration, this will create a `public/js/laroute.js` file to include in your page, or build.
Expand All @@ -88,7 +115,7 @@ With the default configuration, this will create a `public/js/laroute.js` file t
<script src="/js/laroute.js"></script>
```

**Note: You'll have to `generate:laroute` if you change your routes.**
**Note: You'll have to `laroute:generate` if you change your routes.**

## JavaScript Documentation

Expand Down Expand Up @@ -125,6 +152,21 @@ Generate a URL for a given named route.
laroute.route('Hello.{planet}', { planet : 'world' });
```

### url

Generate a fully qualified URL to the given path.

```js
/**
* laroute.url(name, [parameters = []])
*
* name : The name of the route to route to.
* parameters : Optional. value array of route parameters.
*/

laroute.url('foo/bar', ['aaa', 'bbb']); // -> /foo/bar/aaa/bbb
```

### link_to

Generate a html link to the given url.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class TemplateCompiler implements CompilerInterface
public function compile($template, $data)
{
foreach ($data as $key => $value) {
$key = strtoupper($key);
$key = strtoupper($key);

if(is_bool($value))
$value = $value ? 'true' : 'false';

$template = preg_replace("#\\$$key\\$#i", $value, $template);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<?php

namespace Lord\Laroute\Commands;
namespace Lord\Laroute\Console\Commands;

use Config;
use Illuminate\Console\Command;
use Lord\Laroute\Routes\Collection as Routes;
use Symfony\Component\Console\Input\InputOption;
use Lord\Laroute\Routes\Exceptions\ZeroRoutesException;
use Lord\Laroute\Generators\GeneratorInterface as Generator;

use Illuminate\Config\Repository as Config;
use Illuminate\Console\Command;

use Symfony\Component\Console\Input\InputOption;

class LarouteGeneratorCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'generate:laroute';
protected $name = 'laroute:generate';

/**
* The console command description.
Expand All @@ -25,6 +26,13 @@ class LarouteGeneratorCommand extends Command
*/
protected $description = 'Generate a laravel routes file';

/**
* Config
*
* @var Config
*/
protected $config;

/**
* An array of all the registered routes.
*
Expand All @@ -42,20 +50,23 @@ class LarouteGeneratorCommand extends Command
/**
* Create a new command instance.
*
* @return void
* @param Config $config
* @param Routes $routes
* @param Generator $generator
*/
public function __construct(Routes $routes, Generator $generator)
public function __construct(Config $config, Routes $routes, Generator $generator)
{
parent::__construct();

$this->config = $config;
$this->routes = $routes;
$this->generator = $generator;

parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
* @return void
*/
public function fire()
{
Expand All @@ -66,8 +77,8 @@ public function fire()
$this->getFileGenerationPath()
);

return $this->info("Created: {$filePath}");
} catch (NoRoutesException $e) {
$this->info("Created: {$filePath}");
} catch (\Exception $e) {
$this->error($e->getMessage());
}
}
Expand All @@ -79,7 +90,7 @@ public function fire()
*/
protected function getTemplatePath()
{
return Config::get('laroute::config.template');
return $this->config->get('laroute.template');
}

/**
Expand All @@ -89,10 +100,12 @@ protected function getTemplatePath()
*/
protected function getTemplateData()
{
$namespace = $this->getOptionOrConfig('namespace');
$routes = $this->routes->toJSON();
$namespace = $this->getOptionOrConfig('namespace');
$routes = $this->routes->toJSON();
$absolute = $this->config->get('laroute.absolute', false);
$rootUrl = $this->config->get('app.url', '');

return compact('namespace', 'routes');
return compact('namespace', 'routes', 'absolute', 'rootUrl');
}


Expand All @@ -111,14 +124,18 @@ protected function getFileGenerationPath()

/**
* Get an option value either from console input, or the config files.
*
* @param $key
*
* @return array|mixed|string
*/
protected function getOptionOrConfig($key)
{
if ($option = $this->option($key)) {
return $option;
}

return Config::get("laroute::config.{$key}");
return $this->config->get("laroute.{$key}");
}

/**
Expand All @@ -128,24 +145,24 @@ protected function getOptionOrConfig($key)
*/
protected function getOptions()
{
return array(
array(
return [
[
'path',
'p',
InputOption::VALUE_OPTIONAL,
sprintf('Path to the javscript assets directory (default: "%s")', Config::get('laroute::config.path'))
),
array(
sprintf('Path to the javscript assets directory (default: "%s")', $this->config->get('laroute.path'))
],
[
'filename',
'f',
InputOption::VALUE_OPTIONAL,
sprintf('Filename of the javascript file (default: "%s")', Config::get('laroute::config.filename'))
),
array(
sprintf('Filename of the javascript file (default: "%s")', $this->config->get('laroute.filename'))
],
[
'namespace',
null,
InputOption::VALUE_OPTIONAL, sprintf('Javascript namespace for the functions (think _.js) (default: "%s")', Config::get('laroute::config.namespace'))
),
);
InputOption::VALUE_OPTIONAL, sprintf('Javascript namespace for the functions (think _.js) (default: "%s")', $this->config->get('laroute.namespace'))
],
];
}
}
}

0 comments on commit ddcaaa7

Please sign in to comment.