nicksagona on 5.2.6
nicksagona on master
Patch relationship PK issue whe… (compare)
nicksagona on v5-dev
Patch relationship PK issue whe… (compare)
nicksagona on 5.2.5
nicksagona on master
Patch expression parser for whe… (compare)
nicksagona on v5-dev
Patch expression parser for whe… (compare)
nicksagona on 5.2.4
nicksagona on master
Patch BelongsTo FK reference is… (compare)
nicksagona on v5-dev
Patch BelongsTo FK reference is… (compare)
nicksagona on 5.2.3
nicksagona on master
Add support for AFTER in the sc… (compare)
nicksagona on v5-dev
Add support for AFTER in the sc… (compare)
nicksagona on 5.2.2
nicksagona on 5.2.2
nicksagona on master
Update row find method (compare)
nicksagona on v5-dev
Update row find method (compare)
nicksagona on 5.2.2
nicksagona on master
Add support for $options for fi… (compare)
nicksagona on v5-dev
Add support for $options for fi… (compare)
nicksagona on 3.6.3
popphp
components
popphp
components (router, controller) and the view component
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
<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>
<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>
$router->addRoute('/profile/:id [
'controller' => 'MyApp\Controller\IndexController',
'action' => 'profile'
]);
profile($id)
in the MyApp\Controller\IndexController
class like
public function profile($id)
{
echo $id;
}
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.
$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');
$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'
]
]
]);
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:
$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>';
}
$user = Users::with('posts.comments')->getById(1001);
pop-db
v5.2.0