Different way of referencing images/stylesheets based on development and production environment in Zend Framework? - image

I want to host images on separate sub-domain to avoid sending cookies with HTTP request.
There are two domains:
mydomain.com - to serve all the content
static.mydomain.com - to serve images and static content
Both domains are pointing to the same directory hierarchy.
So is there any way to utilize Zend framework feature to:
In development environment reference to static content locally
In production environment use full domain name (static.mydomain.com)

Simplest solution would probably be to create a view helper, something along the lines of:
class My_View_Helper_AssetPath extends Zend_View_Helper_Abstract
{
public function assetPath($filename)
{
if (APPLICATION_ENV == 'production') {
$path = 'http://static.mydomain.com';
} else {
$path = '';
}
$path .= $filename;
return $path;
}
then in your templates:
<img src="<?=$this->assetPath('/images/logo.png')?>" ... />

Related

Kreait\Firebase\Exception\Database\DatabaseError 404 Not Found

I am new to Laravel so i have been trying to connect Laravel to a Firebase Realtime database but i am getting this error Kreait\Firebase\Exception\Database\DatabaseError
404 Not Found . I have the Service account in the controllers directory and properly referenced in the Firebase controller.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Kreait\Firebase;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Kreait\Firebase\Database;
class FirebaseController extends Controller
{
$factory = (new Factory)->withServiceAccount(__DIR__.'/myServiceAccount.json');
$database = $factory->createDatabase();
$newPost = $database->getReference('myrealtime-default-rtdb/blog/posts')->push(['title' => 'Post title','body' => 'This should probably be longer.']);
echo"<pre>";
print_r($newPost->getvalue());
}
}
}
Using the new Factory pattern won't work (it's already handled by the Laravel Service Provider).
Personally I'd recommend using the app() helper. It's quite easy but before that make sure you've followed the installation process in the documentation. You might've missed something
Here's a link to that: https://github.com/kreait/laravel-firebase
Also make sure you include both the path to your Service Account json file and the database URL for your project in your .env file (I prefer using this method personally)
So in your .env file you should have something like
FIREBASE_CREDENTIALS = path_to_your_service_account_file.json
FIREBASE_DATABASE_URL= https://your-project-url.firebaseio.com/
(you can find this url when you open RealTime databases in your firebase console)
If you don't use auto-discovery make sure to add Kreait\Laravel\Firebase\ServiceProvider::class to your providers in the config/app.php file
run a vendor publish
Then in your controller you could have something like this
namespace App\Http\Controllers;
class FirebaseController extends Controller {
//
protected $database;
public function __construct()
{
$this->database = app('firebase.database');
}
public function index(){
$new_post = $this->database->getReference('your-reference')>push(["title" =>"something"]);
} }
Remember: on Linux, place Firebase config files etc. in a folder that user 'apache' can read! So, for example, do not place such files in /home/myname/firebase.json. Even if you do chmod 777 firebase.json, this file may not be accessible by user 'apache', hence the 404.
Then you do not need to use env variables.
$factory = (new Factory())->withServiceAccount(DIR.'/vendor/firebase-adminsdk.json');

Laravel - Generate url for an asset accessed via subdomain

In my Laravel site, I am serving my assets via CDN at https://cdn.example.com/asset.js. If I know the asset name, how can I generate a URL to the asset?
So far I have managed to generate https://example.com/asset.js by doing asset(Storage::url('asset.js'));, but I can't figure out how to add the subdomain, either by calling different methods or editing config.
Any ideas?
Cheers.
put your assets in public folder within or without folder and fetch those assets like css, js or any with
{{ asset('myassset.js') }}
or if in any folder then
{{ asset('myfolder/asset.js') }}
`
Consider creating a custom function that can be called with asset_cdn('asset.js') might a better solution.
function asset_cdn( $asset, $secure = false ){
$protocol = $secure ? 'https:' : 'http:';
return $protocol . '//cdn.example.com/' . $asset;
}
How to make custom function in here.

How can i switch a magento store programatically with seo Url?

How can i switch a magento store programatically with seo Url without creating seperate folder ?.
I have a storeController in my custom module.
Its not working perfectly. current store cookie value not changed because it automatically goes to default store when i go to homepage.
This is what i want ( i need really a store switch effect).
www.site.com/module/store/id/
My Store Controller
class Namespace_Module_StoreController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
// site.com/module/store/12-store1
$id = explode('-',$this->getRequest()->getParam('id'));
$id = $id[0];
$store = Mage::getModel( 'core/store' )->load($id);
$this->_title($this->__('Module'))->_title($store->getName());
$rootCategoryId = $store->getRootCategoryId();
Mage::app()->setCurrentStore($store->getStoreId());
$this->loadLayout();
$this->renderLayout();
}
}
A controller would be far to deep to switch stores at this point. Store run codes can only be set when the application loads at bootstrap level (yes, index.php) and not at any point after (in which the controller dispatches are included).
Try your luck somewhere else.. A good solution is to use .htaccess to set the MAGE_RUN_CODE (to your desired store view) and MAGE_RUN_TYPE (to store/website) environment variables before bootstrapping ever happens.

Can the subdomain URL segment be used as a class in Codeigniter?

I need to use a subdomain as a class. ie, instead of:
www.example.com/class/function/ID eg www.example.com/town-name/history/1
I need
class.example.com/function/ID eg town-name.example.com/history/1
I've wildcarded the subdomain in nginx, now I've googled and read
http://codeigniter.com/user_guide/libraries/uri.html
http://codeigniter.com/user_guide/general/urls.html
http://codeigniter.com/user_guide/helpers/url_helper.html
but nothing relevant. I need it to be so that if another town is added to the db, it'll resolve that new town and its details.
I see many discussions about rewrites, redirects etc, but I specifically need to use the subdomain town name as the class variable. If possible, in an idea world, I could use both together, but I doubt that's possible?
I've had it going fine in plain old php for a couple of years now; now I want to upgrade to codeigniter without ruining my old structure if possible (plus there's a good reason for it).
Thanks you.
You can do it. I'm doing it for one of my projects.
In the constructor of your controller, just explode the current url and get the subdomain and pass it in your method as a variable
public class Controller extends CI_Controller {
$subdomain = null;
public __construct()
{
// explode url here and set $this->subdomain = the actual subdomain
}
public function index()
{
if($this->subdomain == null) {
show_404();
} else {
// do what you wish
}
}
}

Codeigniter 2.0.x - Controller subdirectories default controller needs to be in URL

I recently upgraded a site from CodeIgniter 1.7.x to 2.0.3. About the same time someone in my organization requested we add some pages to the site. Underneath a section. In the old version of the site I used some workarounds in the controller to break up a longer URL. But in version 2 I see that I should be able to use subdirectories in the controllers folder to do it in a more proper way. After looking all over the place I've tried all sorts of routing declarations and fiddled with all sorts of things. Hopefully I'm doing something simple, wrong, or perhaps someone has seen a similar issue stemming from the upgrade.
I'm trying to get the URL from something like:
/about/locations
Which used to work with a controller named about.php. To something more like:
/about/social_responsibility/commitment
Where about is now a aub-directory.
Funny thing is, currently it does sorta work. That second URL displays correctly. However my old pages, that first URL, now do not function... My new structure uses a base.php (default_controller) in the about directory. Thus if I write:
/about/base/locations
It does work. But I thought the whole routing thing (default controller) and using subdirectories is supposed to clean the URL up.
My info is as follows...
Current Routing (it's changed a bunch over the last few hours)
$route['default_controller'] = "base";
$route['404_override'] = '';
$route['about'] = "about/base";
Directories and Files
/controllers/about/base.php
/controllers/about/social_responsibility.php
Chunk of base.php
class Base extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->data['parent'] = "About";
$this->load->model('mnav');
}
public function index()
{
}
public function locations()
{
}
}
I also have a MY_Controller that extends CI_Controller, but all it does is enable FirePHP for me in the development environment.
Anyone have any clues? Or need some more info to help? Thanks!
I just tested on my system, with CI 2.0.2 and it seems that the default controller setting works for subdirectories as well, without any other routes.
// so in your config file, whatever your default_controller is set to...
// you would just use that as the name of the `base` controller in about
// for example, if your default_controller is 'welcome'
// in /application/config.php
$route['default_controller'] = "welcome";
$route['404_override'] = '';
// then, it should work for the subdirectory where there is a controller
// named 'welcome'
// application/controllers/about/base.php
class Welcome extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo "I can be reached with /about";
}
}
So, all you should have to do is remove all of the routes for about
// remove this
$route['about'] = "about/base";
Important
This will only work when accessing /about -- anything in additional segments will be looking for additional controllers. So you'd have to think about how you'd like to access the base controller (whether or not you name it something else).
I'd assume that /about/locations is looking for an actual "locations" controller in your about folder, rather than being the method for your base controller. As far as CI knows, you're trying to execute one of two functions:
About controller's "locations" method
about/Locations controller's index method (obviously doesn't exist)
It follows that any 3-segment or greater URI will work with this scheme, but 2-segment URI's will confuse it. I don't think letting CI fallback to the default controller is going to work here. Try this:
$route['default_controller'] = "base";
$route['404_override'] = '';
$route['([^\/]*)'] = '$1/base';
$route['([^\/]*)/([^\/]*)'] = '$1/base/$2';

Resources