-2

I'm learning Laravel as my first PHP framework and am trying to become better at PHP. In the config files I see the following:

<?php

return ['url' => 'http://localhost','cipher' => MCRYPT_RIJNDAEL_128,'etc'=>'etc'];

(I) How can this file be used?

(II) And why don't they use a regular array like: $myarray = ["a","b","etc"].

Edit: The question is why would one use this approach. This has been answered below.

rinserepeat
  • 165
  • 13
  • 2
    What do you mean a regular array? This is pretty standard PHP, return behaviour from an included file, as per example #5 in the [PHP docs](http://php.net/manual/en/function.include.php).... it's not exactly dark magic – Mark Baker Mar 05 '15 at 00:25
  • Interesting how this is being down voted that easily. Example #5 in the manual uses a Variable $var = 'bla'; return $bla;. The File I'm talking about Doesn't use a Variable it seems, just Return ['stuff"]. Dark magic or not. What i meant with regular array was illustrated too. I was just wondering why one would do this; perhaps speed? yes its not Zend and im not certified either.. – rinserepeat Mar 05 '15 at 00:31
  • 1
    @rinserepeat - read example #5 __properly__..... the first bit with the file called `return.php` the one that uses a `return` statement.... yes it uses a variable, but it could just as easily simply say `return "PHP";` The principle of the return value being assignable in the calling script is exactly the same – Mark Baker Mar 05 '15 at 00:39
  • thank you for answering ill leave question (II) as is and find that info elsewhere – rinserepeat Mar 05 '15 at 00:43
  • 2
    There's nothing wrong with return ['stuff']. It's fewer lines of code to parse, it's perfectly readable, it's straightforward PHP using fully documented behaviour, it saves the overhead of assigning a value to a variable before returning it, and it's a perfectly normal, regular array – Mark Baker Mar 05 '15 at 00:45
  • 1
    It's returning an associative array (key/value). I'm not too familiar with Laravel but it's prob. there so you can just do $foo = include "thatFile.php"; instead of having to remember and re-type that array. – lciamp Mar 05 '15 at 00:45
  • Thanks alot @lciamp That was actually an answer to the question! Cheers. – rinserepeat Mar 05 '15 at 00:47
  • @rinserepeat No prob! I'll put it up in the answers. Cheers :) – lciamp Mar 05 '15 at 00:49

2 Answers2

1

I've been using Laravel for a while now. What you have looks like a snippet for the config/app.php config file.

All it's doing is returning an array. This is part of the Laravel configuration. You don't use it, Laravel does. If you need to change any configuration for Laravel, you'll do it in the php files under the config directory.

Like, if you are adding a plugin for Laravel, you'll install it through composer, and register it with Laravel using the app config.

Example:
Installing the Gravatar plugin for Laravel.

$ composer require thomaswelton/gravatar 1.*

And add the facade and service provider through the app.php config

 <?php
 return array(
      ...
      'providers' => array(
          'Thomaswelton\LaravelGravatar\LaravelGravatarServiceProvider'
      ...
      'aliases' => array(
          'Gravatar' => 'Thomaswelton\LaravelGravatar\Facades\Gravatar'
      ...

That's essentially all these files are for, is the configuration for Laravel.

Pazuzu156
  • 679
  • 6
  • 13
  • Thanks for your reply Kaleb. Thx to lciamp I know understand why one would setup a page like that, havent see a sec return [ ] until today. ;-) – rinserepeat Mar 05 '15 at 00:50
  • 2
    Welcome. Since you're new to the whole Laravel/PHP scene, I'll refer you to Jeffery Way, a master Laravel user. http://laracasts.com You'll find those videos extremely useful. – Pazuzu156 Mar 05 '15 at 00:52
  • @rinserepeat I'm gonna back Kaleb on this, visit laracasts.com . I don't know Laravel but I use PhpStorm and if their "Be Awesome At PhpStorm" video is any representation of what they do, than what they do is awesome. – lciamp Mar 05 '15 at 03:58
  • hehe nice one. I'll have a look at it! thx – rinserepeat Mar 05 '15 at 15:29
  • 1
    @lciamp Sublime Text is my editor of choice, but all my Laravel stuff in done in PhpStorm. And yes, that series is awesome, and extremely informative, just like all the videos there. There's also videos on Composer, which Laravel is a composer package, and thus uses composer for a lot. Another series I recommend watching as well. – Pazuzu156 Mar 06 '15 at 05:06
  • @KalebKlein Nice! I have off next week, I'm gonna check some of them out!! – lciamp Mar 12 '15 at 01:21
1

It's returning an associative array (key/value). I'm not too familiar with Laravel but it's prob. there so you can just do

$foo = include "thatFile.php"; 

instead of having to remember and re-type that array.

lciamp
  • 675
  • 1
  • 9
  • 19