Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Oct 19 16:42

    nicksagona on 5.2.1

    (compare)

  • Oct 19 16:36

    nicksagona on master

    Patch empty rows issue for "has… (compare)

  • Oct 19 16:36

    nicksagona on v5-dev

    Patch empty rows issue for "has… (compare)

  • Oct 13 22:18

    nicksagona on 3.5.4

    (compare)

  • Oct 13 22:15

    nicksagona on master

    Patch error checking for single… (compare)

  • Oct 13 22:15

    nicksagona on v3-dev

    Patch error checking for single… (compare)

  • Oct 13 21:43

    nicksagona on master

    Add ability for FormValidator t… (compare)

  • Oct 13 21:43

    nicksagona on v3-dev

    Add ability for FormValidator t… (compare)

  • Oct 09 03:41

    nicksagona on 5.2.0

    (compare)

  • Oct 09 03:38

    nicksagona on master

    Add support for child relations… (compare)

  • Oct 09 03:33

    nicksagona on v5-dev

    Add support for child relations… (compare)

  • Oct 02 21:55

    nicksagona on 5.1.0

    (compare)

  • Oct 02 21:54

    nicksagona on v5-dev

    Patch eager relationships optio… (compare)

  • Oct 02 21:54

    nicksagona on master

    Patch eager relationships optio… (compare)

  • Oct 02 21:50

    nicksagona on master

    Add options support to eager re… (compare)

  • Oct 02 21:50

    nicksagona on v5-dev

    Add options support to eager re… (compare)

  • Sep 24 19:15
    nicksagona closed #22
  • Sep 24 19:13

    nicksagona on 3.5.3

    (compare)

  • Sep 24 19:11

    nicksagona on master

    Break out a setValues method fo… (compare)

  • Sep 24 19:10

    nicksagona on v3-dev

    Break out a setValues method fo… (compare)

Nick Sagona
@nicksagona
and that's supported for console app routing as well
and then those routes can map to callable items (closures, actions in a controller, etc) that take those parameters in like:
function ($id) { }
but as far as the URL/HREF generation
that's not really available in the core popphp components
Nick Sagona
@nicksagona
but something may be possibly wired up in the MVC between the core popphp components (router, controller) and the view component
petkovic-v
@petkovic-v

Ok. Understand . Sorry if I'm holding you back too much and thanks again for trying to help me. I read all your documentation about the PopPHP framework in the last two days and I didn't find what I asked as a question here. The PoPPHP framework is very nice and I would like to move on from Laravel to use it.

Let me try to ask you like this. In PopPHP routes we can define as follows:

$config = [
'routes' => [
'/products/:id' => [
'controller' => 'MyApp\Controller\ProductsController',
'action' => 'index',
'controllerParams' => [
'id' => 789
]
]
]
];

$app = new Pop\Application($config);

We have a defined route in the code above that responds to the url address /products/:id. The Route object has the properties controller, action, and controllerParams (but as far as I can see property name does not exist as in Laravel for the reverse process - generating url addresses via route object).

In PoPPHP, I assume that this route can be accessed using the code:

$myRoute = $app->router()->getRoutes()['/products/:id'];

There was a possibility to generate a url address from the route (for example for the href Edit button in the html table in the 3rd row the generated url address would be).

/products/3 - for dispaly details page for product 3

Thank you for all the answers you have given me. Greetings and I wish you all the best.
Nick Sagona
@nicksagona
No problem - and you're not holding me back at all. Happy to help. I'm currently at work right now, but I will take a look at the example you just posted and see if I can get an answer for you later this evening.
Nick Sagona
@nicksagona
So for clarity, you are asking about generating the URLs for the HREF attribute in A tags inside of an html view, correct? For example, a simple table like:
<table>
    <tr>
        <td><a href="/profile/1">Edit</a></td>
        <td>Profile 1</td>
    </tr>
    <tr>
        <td><a href="/profile/2">Edit</a></td>
        <td>Profile 2</td>
    </tr>
    <tr>
        <td><a href="/profile/3">Edit</a></td>
        <td>Profile 3</td>
    </tr>
</table>
and you want to use the router to generate those URL strings in that HTML there, is that correct?
If that's the case, the router isn't really utilized for that in PopPHP. That would be something would just handled by the view component with a data set like:
<table>
<?php foreach ($profiles as $profile): ?>
    <tr>
        <td><a href="/profile/<?=$profile['id']; ?>">Edit</a></td>
        <td>Profile <?=$profile['id']; ?></td>
    </tr>
<?php endforeach; ?>
</table>
Nick Sagona
@nicksagona
Also for a deeper dive, the controller params are intended to be more for top-level controller properties that the controller might need within the life cycle of any request that it handles. Take a look at the tutorial app here:
and you can see the the tutorial app is wired up that all controllers get passed the controller params of a request and a response object
plus the application object as well, as those objects would be needed on just about every request.
from there, there are dispatch params (which I think is more what you are talking about.) And those are the individual params that get passed down from the request into the action method of the controller
for example, the route of:
$router->addRoute('/profile/:id [
    'controller' => 'MyApp\Controller\IndexController',
    'action'     => 'profile'
]);
would expect there to be a method profile($id) in the MyApp\Controller\IndexController class like
public function profile($id)
{
    echo $id;
}
Nick Sagona
@nicksagona
the router itself would not be aware of any set of potential routes (/profile/1, /profile/2, etc.) that are available in the system. It's simply reactive to all requests coming through and handles the logic to figure out if the route is a valid route or not and which controller/action to hand it off to. It would just know that any route of /profile/:id is valid and to pass it off to the controller from there
Let me know if that helps any. Thanks!
petkovic-v
@petkovic-v

Yes, Mr. Nick that's what I want to achieve as an output Html. I apologize in advance for my poor knowledge of English and if it bothers you to clearly understand what I want to ask.

However, if I may say so, you are hard coding the url in the foreach loop (at least as far as the url part /profile/ is concerned) and this is considered bad practice.Why?

I'm actually interested in something called "bidirectional routing"

here is some definition :

"Thanks to bidirectional routing, you'll never have to hardcode application's URLs in the templates(Views) or code, which may change later or be complicated to compose. Just specify the Controller name and the action name , pass any parameters (for genereting dynamic segments url and query string variable) and the framework (Laravel , Slim ....) will generate the URL itself. "

The point is that urls not recommended creating by hard coding (like a string - href ="myWebApp/segment1/segment2?var1=1") because if for some reason a url needs to be changed and if that url address is used in 20 places in a web application, it is a very tedious job prone to errors.

therefore, bidirectional routing (as far as I understand) allows us to define route and url string (url templates) and also we can define which controller and its action method will react to that url address.(this is how routes are defined in all mvc frameworks, and in POPPhp also).

But in Laravel and Slim,continue on, and we can give each route a name. Why is the name important?

In Laravel, for example, there is a function route() which accepts two parameters: the name of the route (The name is used to access the route) and options array.In options array we can define parameters for dynamic route segments and for the query string variable.the route function automatically generates and returns a url string relative to the arguments passed to it.

example :

Route::get('/post/{post}', function () {
    //
})->name('post.show');

To generate a URL to this route, you may use the route helper like so:

echo route('post.show', ['post' => 1]);

// http://example.com/post/1   - genereting concrete url string

In this way, in Laravel, we MUST hard coding the url address ONLY IN ONE PLACE - in the route definition and if we now need to use that ulr address in, say, 20 places in a web application, then instead of using a hard-coded url string, we call the route (routeName, opt) function in 20 places, which will generate the url string, given the arguments passed (for example id is read from the database and used in foreach loop to generate).

If the url needs to be changed tomorrow for any reason, it only needs to be changed in one place (in the route definition) and that change will be automatically reflected in all 20 places in the application.

So, ...
Is there such a function in the popphp framework that we pass the name of the controller , the name of the action method of the controller and parameters (for dynamic url segments and variable of the query string) and that function then generates and returns the url string address?
If POPPHP had a route name property, then neither the name of the controller, nor the name of its method would have to be passed to route() function , but only the name of the route because the name of the controller and its methods are automatically read from the route definition.

Nick Sagona
@nicksagona
ok I understand what your asking about. You're asking about URL generation (from the router)
and at this time, that is not available, but is definitely something that can be considered.
petkovic-v
@petkovic-v
Yes, that's right. Thank you for all the answers.
Nick Sagona
@nicksagona
No problem. Like I said, we can consider adding this functionality.
Nick Sagona
@nicksagona
@petkovic-v okay, I've got a quick proof of concept up and running for URL generation coming from the Router component in the core popphp repo
if you want, you can check the repo out and checkout to the v3-dev-7 branch and see what's going on.
A quick run down on how it's working initially
you can add named routes either programmatically or via the application route config:
option 1:
$router = new Pop\Router\Router();

$router->addRoute('/home', function() {
    echo 'Home!' . PHP_EOL;
})->name('home');

$router->addRoute('/hello/:name', function($name) {
    echo 'Hello, ' . $name . '!' . PHP_EOL;
})->name('hello');
option 2:
$app = new Application([
    'routes' => [
        '/home' => [
            'controller' => function () {
                echo 'Home!' . PHP_EOL;
            },
            'name' => 'home'
        ],
        '/hello/:name' => [
            'controller' => function ($name) {
                echo 'Hello, ' . $name . '!' . PHP_EOL;
            },
            'name' => 'hello'
        ]
    ]
]);
either should work the same
there is also now a global/static Route class that acts like a custom service container of the router object for the application. With that, anywhere in the application you can do this:
(this works with arrays or objects)
$nick = new stdClass();
$nick->name = 'nick';

$jim = new stdClass();
$jim->name = 'jim';

$world = new stdClass();
$world->name = 'world';

$names = [$nick, $jim, $world];

foreach ($names as $name) {
    echo '<a href="' . Route::url('hello', $name) . '">'  .  $name['name'] . '</a>';
}
and that should generate:
Nick Sagona
@nicksagona
<a href="/hello/nick">nick</a>
<a href="/hello/jim">jim</a>
<a href="/hello/world">world</a>
petkovic-v
@petkovic-v

Yes, that's what we need, Nick.

Route :: url (nameOfRoute, Parameters)

Thank you so much for your efforts to resolve this.

Nick Sagona
@nicksagona
ok great. if this provides everything you were looking for in this feature, I'll go ahead and merge it into master and cut a release a little later on today. Thanks for your input on this.
Nick Sagona
@nicksagona
Ok the new named routes and URL generation features have been released under the popphp 3.6.0 release. The docs have been updated as well.
Nick Sagona
@nicksagona
Hey everyone - quick announcement for an added feature to pop-db
Relationships now support nested/child relationships via a dot notation:
$user = Users::with('posts.comments')->getById(1001);
you can read more about it under the docs here (under Eager-loading) https://pop-php-framework.readthedocs.io/en/latest/user_guide/databases.html#relationships-associations
this is available in pop-db v5.2.0