Laravel 5.0 -Dev Defining Global Patterns is not working - url-routing

Laravel Docs provide a way to add global patterns inside the before function inside RouteServiceProvider.php.
my question is : by default there is no such function, besides, after creating it, it's not working!
/**
* Define global rules for routes.
* more specially for regullar expressions.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function before(Router $router){
$router->pattern('id', '[1-9]+[0-9]*');
}

Just in case you're still interested (I dont have enough points to comment), you can still declare it inside boot() but just before the $route variable is passed to the parent parent::boot($router); function, like here:
/**
* Define your route model bindings, pattern filters, etc.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function boot(Router $router)
{
///////////////////////////
// route global patterns //
///////////////////////////
$router->pattern('id', '[0-9]+');
parent::boot($router);
}
This worked for me

I had the same problem and what I did was to append the before() method body into the map() method body. It worked for me. :)

Related

CakePHP with PHPStan: Property does not accept Cake\ORM\Table

Working my way through the levels of PHPStan with a new applicaton, I got to level 3 and started getting error messages from all my test fixtures for models. The basic format is as follows:
------
Line tests/TestCase/Model/Table/UsersTableTest.php
------
43 Property Visualize\Test\TestCase\Model\Table\UsersTableTest::$Users (Visualize\Model\Table\UsersTable) does not accept Cake\ORM\Table.
------
The code that this error refers to is:
/**
* setUp method
*
* #return void
*/
public function setUp(): void
{
parent::setUp();
$config = $this->getTableLocator()->exists('Users') ? [] : ['className' => UsersTable::class];
$this->Users = $this->getTableLocator()->get('Users', $config);
}
This setup code was build using cake bake, so I'm not sure what it's looking for. Does anyone else know what will resolve this issue for me?
EDITED: I did a bit of further searching. The only version of the getTableLocator() function I could find associated with this stack was in the TableRegistry class. That class in turn has a function called get() and that function does indeed return an object of type \Cake\Orm\Table:
/**
* Get a table instance from the registry.
*
* See options specification in {#link TableLocator::get()}.
*
* #param string $alias The alias name you want to get.
* #param array $options The options you want to build the table with.
* #return \Cake\ORM\Table
* #deprecated 3.6.0 Use {#link \Cake\ORM\Locator\TableLocator::get()} instead. Will be removed in 5.0.
*/
public static function get(string $alias, array $options = []): Table
{
return static::getTableLocator()->get($alias, $options);
}
So does this mean my tests ought to expect the \Cake\ORM\Table class? TBH, I've yet to do much of anything in the way of testing Models (as you might have guessed), thus I'm not sure the consequences of doing that.
The question is how to deduce from $this->getTableLocator()->get('Users', $config); that it should be returning Visualize\Model\Table\UsersTable.
You can write a dynamic return type extension if you come up with logic that can deduce that from the abstract syntax tree and maybe other places like configuration.
It's possible that the extension https://github.com/CakeDC/cakephp-phpstan might already tackle that, this class definitely looks like that: https://github.com/CakeDC/cakephp-phpstan/blob/master/src/Type/TableLocatorDynamicReturnTypeExtension.php

PHPDoc: append methods to the current class, as if it were extending a class [duplicate]

Is there a way to document that a certain class has magic methods for every method defined in another class?
I am using PhpStorm, so I would be happy with any solution that will get autocomplete to work properly for that.
class A
{
// a bunch of functions go here...
}
/**
* Class B
* What should go here to make it work???
*/
class B
{
private $aInstance;
public function __construct() {
$this->aInstance = new A();
}
public function __call($name, $arguments) {
// TODO: Implement __call() method.
if(method_exists($this->aInstance, $name)) {
return $this->aInstance->{$name}(...$arguments);
}
throw new BadMethodCallException();
}
// a bunch more functions go here...
}
The proper solution is to use supported #method PHPDoc tags. This way it will also work in other editors/IDEs that support PHPDoc and understand such standard tag.
This approach requires every method to be listed separately. More on this in another StackOverflow question/answer: https://stackoverflow.com/a/15634488/783119.
In current PhpStorm versions you may use not-in-PHPDoc-specs (and therefore possibly PhpStorm-specific) #mixin tag.
Adding #mixing className in PHPDoc comment for your target class should do the job for you.
/**
* Class B
*
* #mixin A
*/
class B
{
Basically, #mixin tag does what actual PHP's traits do.
Please note that there is no guarantee that support for such tag will not be removed at some point in the future, although it's pretty unlikely.

Sonata Doctrine Phpcr Admin Bundle: Change parent of node

I have problem with changing parent of document inside sonata admin.
My document has property like this:
/**
* #PHPCRODM\Children(filter="teaserimage*", cascade="persist")
* #var Image[]
*/
protected $teaserImages;
....
/**
* #Validation\PhpcrImage(maxWidth="1500", maxHeight="844", minWidth="600", minHeight="338", minRatio="1.77", maxRatio="1.78", nullable=false)
*
* #return Image|null
*/
public function getTeaserImage()
{
return $this->getLocalizedImage($this->teaserImages, 'teaserimage');
}
/**
* #param Image $image
*/
public function setTeaserImage($image)
{
$this->teaserImages->add(
self::setupImage($image, $this->getLocalizedImageNodeName('teaserimage'), $this->getTeaserImage())
);
}
When i tried to change parent of any article document i got error like
The server returned a "500 Internal Server Error".
The Id is immutable (/cms/content/blog/blog-post-for-12th-october/teaserimage_de.jpg !== /cms/content/blog/blog-post-for-12th-october). Please use DocumentManager::move to move the document: blog post for 12th October (/cms/content/blog/blog-post-for-12th-october)
Although this error occurs, my document has been moved to the right place with all subdocument.
this is how my document looks like
https://gist.github.com/milosnovi/a83f400c8ff06b4de6dd96e1f149a8dd
Check your preUpdated, prePersists methods. You shouldn't flush object in these methods while the change is a parent change.

how to inject a variable in a OneToOne relation entity

I have a File entity for handle files upload in other entities (news/blog/etc).
I point to it with a OneToOne relation and it works fine. But I would change the upload dir, for each relation entity :
upload/news
upload/blog
The upload path is set in my file entity so i dont know how to automaticaly update the path foreach relations...
do you have an idea on how to do it ?
Thanks
Of course you can do it.
On your file entity side, you can add a uploadDir attribute, and create a setter like this :
private $uploadDir;
public function setUploadDir($uploadDir)
{
if (!$this->uploadDir) {
$this->uploadDir = $uploadDir;
}
}
your comment suggest you use Symfony with Doctrine right ?
So you can edit the classical getUploadDir() method like this:
protected function getUploadDir()
{
return 'uploads/' . $this->uploadDir;
}
In the 'parent' entity you have to update this attribute (when it is created) before persist or update.
(I personally use life cycle callbacks but you can do it manually in your controller)
use Doctrine\ORM\Mapping as ORM;
/**
* News
*
* #ORM\Table(name="News")
* #ORM\HasLifecycleCallbacks
*/
class News
{
//....
/**
* #ORM\OneToOne(targetEntity="File",cascade={"persist","remove"})
*/
private $file;
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
function setUploadDir()
{
$this->getFile()->setUploadDir('news');
}
// ....
You can also add a constant to make the code cleaner...
I hope it'll help you

ExtDoc + #private Properties

I'm using ExtDoc [code.google.com/p/ext-doc/] to generate the Documentation associated to a Javascript Application.
The problem is that when I try to declare a Private Property, that Property will never appear in the generated documentation.
This is the used annotation
/**
* This is a Private Property
* #type String
* #property name
* #private
*/
ExtDoc is outdated and no more maintained. Instead use JSDuck, which is the new official docs generator of Ext JS.

Resources