I have installed laravel latest version ..In my application i have used barryvdh/laravel-dompdf package. And i made some font changes in the pdf packages..
Now my problem is while giving composer update this "barryvdh/laravel-dompdf" do not get update ..It should be ignore then only my changes are not affected..
composer update
And here is the package I tried to add to my project: https://github.com/barryvdh/laravel-dompdf
Any suggestions ?
You should never make any changes in classes inside vendor. You should extend those classes instead to change standard functionality.
#Trent is not so difficult to explain.
if you read about OOP, you will get the idea about hierarchy, which means that a class can have a "father" which it extends.
So in this case the vendors in Laravel are loaded by default. So lets say you have a vendor class called "FatherClass", if you want to extend the functionality of it you can create a "ChildClass" extending of it. This is a simple example:
<?php
Class FatherClass{
public function method_one(){
return "Hi, This is method 1";
}
}
Class ChildClass extends FatherClass{
public function method_two(){
return "Hi, This is method 2";
}
}
//So now you can create a child object and will have the father and its own methods.
$childObject = new ChildClass();
$childObject->method_one(); // Hi, This is method 1
$childObject->method_two(); // Hi, This is method 2
?>
So how is this good to understand it for vendors?
Well in your case this is the class that you should extend PDF (https://github.com/barryvdh/laravel-dompdf/blob/master/src/PDF.php).
So it should be something like :
<?php
class NewPdfClass extends PDF{
}
You should be able now to override the methods or create new ones for your purposes.
Let me know if this helps.
Related
I am new to Laravel and would appreciate any help on describing how a Laravel package residing in vendor folder can be extended and wont get affected if I update the package.
I'll try to create a brief guide, and we'll expand if need be.
I suggest putting all of your files inside a separate directory / namespace. You'll benefit from this should you decide to create your own composer package afterwards.
As an example I'll extend bumbummen99/shoppingcart package which forks the great gloudemans/shoppingcart, adding support for Laravel 5.8 and some minor functionality. You will of course first need to install that package:
composer require bumbummen99/shoppingcart
Start by making a couple of folders. You can use any name for folders / classes, this is what I used, relative to the project root:
app/Repositories/ExtendedCart
app/Repositories/ExtendedCart/Facades
Create the file
app/Repositories/ExtendedCart/ExtendedCart.php
This class will extend the package's main class:
namespace App\Repositories\ExtendedCart;
use Gloudemans\Shoppingcart\Cart;
class ExtendedCart extends Cart
{
public function myMethod(){
return 'myMethod';
}
}
Then make your Service Provider. Create the file:
app/Repositories/ExtendedCart/ExtendedCartServiceProvider.php
(I'm not using artisan as generating / moving the provider will produce wrong namespace)
This is your Service Provider content, here you reference the class that extends the package's class. Note you overwrite the binding from the original package.
namespace App\Repositories\ExtendedCart;
use Gloudemans\Shoppingcart\ShoppingcartServiceProvider;
class ExtendedCartServiceProvider extends ShoppingcartServiceProvider
{
public function register()
{
$this->app->bind('cart', 'App\Repositories\ExtendedCart\ExtendedCart');
}
}
Then register your service provider in config/app.php
'providers' => [
...
//Add this line to the end of providers array
App\Repositories\ExtendedCart\ExtendedCartServiceProvider::class,
]
Lastly create a Facade, which will instantiate the class (otherwise you will get non-static method exceptions). Create this file:
app/Repositories/ExtendedCart/Facades/ExtendedCart.php
This is the contents of the file:
namespace App\Repositories\ExtendedCart\Facades;
use Illuminate\Support\Facades\Facade;
class ExtendedCart extends Facade {
protected static function getFacadeAccessor() { return 'cart'; }
}
You're all set to use your extended methods. You can safely upgrade the original package, and you can even use the default facade:
namespace App\Http\Controllers;
use Cart;
class SomeController extends Controller{
public function someFunction(){
Cart::instance('default')->myMethod();
//This should return 'myMethod'
}
}
I hope this helps, good luck!
I want to extend CI_Model:
<?php
class ModelBasic extends CI_Model
{
}
?>
It won't be autoloaded unless under such a path:
./application/core/MY_Model.php
It seems name of MY_Model.php is mandatory. Now my problem is that when I create MY_Model.php CodeIgniter Expects me to have a model under name of MY_Model which I want to avoid. Can I have my own custom model name without making a fake class to suppress this error?
Fatal error: Class 'MY_Model' not found in /var/www/CodeIgniter/system/core/Common.php on line 174
The quickest way to achieve naming your own custom base model(s) without any additional modification is to create the file application/core/MY_Model.php, define an empty MY_Model class, and then create your own custom class(es):
class MY_Model extends CI_Model {}
class ModelBasic extends MY_Model {
// Your code here
}
// Define more than one if you want.
class ModelComplicated extends MY_Model {
// Your code here
}
Other options include:
Extend the Loader class, and replace the model() method with your own modified version. This could be complicated (I haven't explored it fully), since the actual error is occurring in system/core/Common.php :: load_class(), which, in the model's example, is instantiating the base CI_Model class, as well as MY_Model if it finds one (which it automatically looks for by default, like other base classes).
Create / Add an autoloader that follows your own rules for loading core classes (may be complicated if you try to autoload more than just models -- really depends on how you want to set your app up).
Extend the Loader class and re-write that part of the code.
I'm quite new to ZF, and right now, i try to write a tiny app, based on ZF. It works more or less fine until now. I wanna access my db- data. For starters, i just want to use query-string, before I start messing araound with zend_db. So to keep a nice mvc-structure, I created application/models/IndexMapper.php
class Application_Models_IndexMapper{...}
it just contains one function by now to see if it works
public function test(){
return ('yay');
}
In my IndexController, which is working, i try to access my model by
$indexMapper = new Application_Models_IndexMapper();
$x = $indexMapper->test();
but the first line throws an
Fatal error: Class 'Application_Models_IndexMapper' not found in /path/to/application/controllers/IndexController.php on line 31
As I'm new, I don't understand the more complex tutorials and they don't help me fix my problem. What am I doing wrong? Do I have to include it somehow?
Thanks
edit: my application/bootstrap.php
<?php
defined('APPLICATION_PATH')
or define('APPLICATION_PATH' , dirname(__FILE__));
defined('APPLICATION_ENVIRONMENT')
or define('APPLICATION_ENVIRONMENT' , 'development');
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(APPLICATION_PATH . '/controllers');
$frontController->setParam('env', APPLICATION_ENVIRONMENT);
Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts');
//Doctype
$view = Zend_Layout::getMvcInstance()->getView();
$view->doctype('HTML5');
$view->addHelperPath('App/View/Helper', 'App_View_Helper');
unset($frontController);
The structure for a model would be found under ./application/models/IndexMapper.php. In that file you would have the class As you named it and then the autoloading will work.
A great beginner tutorial would be found on www.akrabat.com
You have your class in the wrong place and have named it incorrectly.
Your class should be in application/models/Indexmapper.php and should look like this:-
class Application_Model_Indexmapper
{
public function test(){
return ('yay');
}
}
Then you call it thus:-
$indexMapper = new Application_Model_Indexmapper();
$x = $indexMapper->test();
Notice I dropped the 's' from the end of Models, it is not required and will cause an error as you found. Also the class is in the models folder, not modules. If you want to use modules then you need to read this and this from the manual.
Your bootstrap.php should look like this for a first, basic project:-
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
//Yes, it's empty!
}
Well, I guess my tutorial wasn't very helpful. I'll do as recommended, and start over from scratch. Thanks though
I would like to have a method available to all Views in my app.
I would like to be able to make calls like this:
<span>${ getDynamicText() }</span>
The most obvious ways (to me) to implement this are:
Call the method in the controller and and pass it to the View.
Make the method static on some Util class and call it from the code ${ UtilClass.getDynamicText() }
Using meta programming to somehow make the method available to all Views.
The benefit of #3 is that the change would only have to be made in one place. #1 would have to be made in each controller action; and #2 would need an import on every View page which wants to use the method.
So is there a way to add a method to be available to all views in my app?
I have to admit I don't know in a lot of detail how .gsp files are processed behind-the-scenes so maybe they don't have a corresponding class and therefore can't be manipulated in this way. Links to good articles/docs will get extra good karma.
GSPs are compiled into classes that extend org.codehaus.groovy.grails.web.pages.GroovyPage, so you can add methods to that metaclass and they'll be available to all GSPs. The best place to do this is in BootStrap.groovy (or in a plugin's doWithDynamicMethods closure):
import org.codehaus.groovy.grails.web.pages.GroovyPage
class BootStrap {
def init = { servletContext ->
GroovyPage.metaClass.getDynamicText = { ... }
}
}
The recommended way to reuse functionality across GSPs is to define it as a tag, e.g.
class MyTagLib {
static namespace = 'my'
def dynamicText = {attrs ->
out << 'not very dynamic'
}
}
You can then call this tag in a GSP using:
<my:dynamicText/>
4th way: make a class/service that have method '.getDynamicText' and put it's intance into request at before filter ( request.setAttribute('x', myDynamicTextGeneratorObject) )
Now you can use x.dynamicText in any GSP
This is how I would do it:
Add a new class to your controllers folder containing your method
Do a grails install-templates
Navigate to the templates: \src\templatesscaffolding
Add the extends part to the controller template: class ${className}Controller extends NewController
re-generate your controllers
You can now use the method in every class and gsp.
I've never worked with Zend Framework before, but I've worked with others (CodeIgniter, Kohana, etc). Right now I was asked to just show a view that wasn't existing so I started looking into the documentation and examples I could find and I've always found examples that use the Model part of the MVC, but in this case I just need to load a view and I can't figure out how to do that. I have this:
File: "BookController":
require_once("Initiate.php");
class BookController extends Initiate {
public function init() {
parent::init();
}
public function bookAction(){
#$client = Zend_Auth::getInstance()->getIdentity();
$view = new Zend_View();
echo $this->view->render('book.phtml');
#$this->view->assign("book", $client);
#echo $this->view->render('book.phtml');
}
the view is called "book.phtml" and is found in /application/views/scripts/bookapi/
What am I missing?
Given that eveything is setup correctly with MVC, your Controller should extend Zend_Controller_Action:
class BookController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->funnyText = 'This is a funny text.';
}
}
Then, in your application/views/scripts/book/ folder, there has to be a index.phtml. Which could look like that:
<p>
<?php echo $this->funnyText; ?>
</p>
That's it, nothing more required.
Btw. doesn't make sense to have a controller called book and then also an action called book
you don't have to manage view yourself there is an Action Controller Helper called View Renderer it does your job for you all you need to follow is its naming convention i.e if your controller name is 'BookController' then its view file should be located at views/scripts/book/boo.phtml .
Assuming BookController extends Zend_Controller, it should automatically setup the view and you shouldn't have to render it. Your view file should be in /application/views/scripts/book/book.phtml. Follow the quick start for more information.