october cms: relations

Resources: https://octobercms.com/docs/database/relations https://octobercms.com/docs/backend/relations Example: This is a (hopefully correct) working example of a 1 to n relation. // product model public $hasMany = [ ‘variants’ => [‘\Acme\Namespace\Variant’], ]; // variant model public $belongsTo = [ ‘product’ => [‘\Acme\Namespace\Product’], ]; public $attachOne = [ ‘my_image’ => [‘System\Models\File’], ]; # variant fields.yaml Read more…

install mongodb for php7

sudo apt-get install php7.0-dev ## if apt-get is unable to finde the package # sudo apt-get install ppa-purge # sudo ppa-purge ppa:ondrej/php-7.0 # sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php # sudo apt-get update # sudo apt-get install php7.0-dev pecl -v ## if you see warnings like “unable to find pdo_mysql.so” etc. reinstall Read more…

Forms with Laravel 5

Some components have been removed from Laravel core with version 5. For example the Forms & HTML component. Good to see them again here: https://laravelcollective.com Example form: {{ Form::open([‘route’=>’item.store’, ‘class’=>’form-horizontal’]) }} <div class=”form-group”> <label for=”name” class=”col-sm-2 control-label”>Name</label> <div class=”col-sm-10″> {{ Form::text(‘name’, null, [ ‘class’=>’form-control’, ‘id’=>’name’, ]) }} </div> </div> <div class=”form-group”> <label Read more…

Laravel Lumen: NotFoundHttpException after installation

After a fresh installation of Laravel Lumen, I got a NotFoundHttpException for the index route. I copied the .env.example  file to .env  and set up the APP_KEY. But I still got the NotFoundHttpException. After searching the internet for some time I found out these steps: In public/index.php change the last line to this: $app->run($app[‘request’]); And to make the Read more…

File upload by ajax with Laravel and jQuery

Some Controller method in Laravel 4.2, with some JSON responses: public function postUpload() { Input::file(‘uploadfile’)->move(storage_path(‘temp/’), ‘import.xls’); try { Artisan::call(‘importexcelfile’, [ ‘file’ => ‘app/storage/temp/import.xlsm’, ]); return Response::json([ ‘result’ => 1 ]); } catch (Exception $e) { return Response::json([ ‘result’ => -1, ‘message’ => $e->getMessage(), ]); } } Some jQuery code, which handles Read more…

Call an Artisan Command from anywhere

Small note on how to call an Artisan Command from a Controller or somewhere else: # example 1 Artisan::call(‘files:zip’, [ ‘ziparchiv’ => base_path(‘export/archiv.zip’), ‘files’ => base_path(‘export/*.json’) ]); # example 2 Artisan::call(‘files:export’, [ ‘–remove’ => true, ‘path’ => base_path(‘export/’) ]);  

silex and the trailing slash

Wrong route definition: $app->get(‘/demo’, function() use($app) { return “demo”; }); Right route definition: $app->get(‘/demo/’, function() use($app) { return “demo”; }); The .htaccess file: <IfModule mod_rewrite.c> Options -MultiViews RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f # RewriteRule ^ index.php [QSA,L] RewriteRule ^ index.php [QSA,L] </IfModule>  

Silex: routing to hostname

This is an example how to split your silex project by different hostnames. $app->get(‘/’, function(Request $request) use ($app) { return “foo.localhost.com”; })->host(“foo.localhost.com”); $app->get(‘/’, function(Request $request) use ($app) { return “bar.localhost.com”; })->host(“bar.localhost.com”); Docs: http://silex.sensiolabs.org/api/Silex/Route.html#method_host

some Twig functions / helper

Template function to generate asset URLs Usage like this: <link rel=”stylesheet” href=”{{ assets(‘styles.css’) }}”> And here is the Twig function: $app[‘twig’]->addFunction(new \Twig_SimpleFunction(‘assets’, function ($uri = ”) use ($app) { return ‘//’ . $app[‘request’]->getHttpHost() . $app[‘request’]->getBasePath() . ‘/assets/’ . trim($uri, ‘\/’); })); Template function for reading session value with dot annotation Access session Read more…

Twig asset helper

This function generates an URL for project assets. $app[‘twig’]->addFunction(new \Twig_SimpleFunction(‘asset’, function ($uri = ”) use ($app) { return ‘//’ . $app[‘request’]->getHttpHost() . $app[‘request’]->getBasePath() . ‘/PATH_TO_ASSETS/’ . trim($uri, ‘/\’); })); Usage: {{ asset(‘css/styles.css’) }} {{ asset(‘js/main.js’) }} Here are other solutions: http://silex.sensiolabs.org/doc/cookbook/assets.html

eloquent: prevent duplicate rows

To prevent duplicate database entries there is a built-in method in Eloquent. // data to insert $data = [ ‘col1’ => ‘A’, ‘col2’ => ‘B’ ] // perform a check over col1 and col2 and insert/update the data $instance = MyModel::updateOrCreate([ ‘col1’ => $data[‘col1’], ‘col2’ => $data[‘col2’] ], $data);

PHP profiling with Xdebug

Best is to enable Xdebug with a trigger, that is added by a .htaccess. With this method I can profile a single application within my development environment. Xdebug settings xdebug.profiler_enable_trigger = 1 xdebug.profiler_enable = 0 xdebug.profiler_output_dir = /var/www/html/xdebug/ To make use of Xdebug I need to add a GET, POST or COOKIE variable with Read more…

Laravel: resolve the logged in user from the IoC container

Behind the scenes, the Auth facade is bound to Illuminate\Auth\AuthManager. Means, you need to resolve the AuthManager. This example resolve the currently logged in user from the IoC container: use Illuminate\Auth\AuthManager; class MyController extends \BaseController { protected $user; public function __construct(AuthManager $auth) { $this->user = $auth->user(); } } Link to reference: http://laravel.com/api/4.2/Illuminate/Auth/AuthManager.html

PHP and MS Excel

There are several libraries out there for reading and writing MS Excel files. Here I note down my experiences with some of them. Here is a list of libraries for working with Excel: https://packagist.org/packages/maatwebsite/excel https://packagist.org/packages/php-excel-reader/spreadsheet-reader http://phpexcel.codeplex.com   maatwebsite/excel Link: https://packagist.org/packages/maatwebsite/excel Reading a worksheets first column: Be sure to configure the Read more…