cakephp url modification: remove action and add slug inflector - url

I'm trying to remove the action in the cakephp url and add a slug inflector, to be more clear this is my expected output:
from this: example.com/posts/view/81/This is a test post
to this: example.com/posts/This-is-a-test-post
This is my current code:
That gives me this output: example.com/posts/view/This is a test post
Controller:
public function view($title = null) {
if (!$title) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Post->findByTitle($title);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
view.ctp:
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['title']));
I also tried this one,this link being my reference CakePHP: Use post title as the slug for view method :
Output: example.com/posts/view/21/This-is-a-test-post
Controller:
function view($id) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
view.ctp
$this->Html->link($post['Post']['title'], array('action' => 'view', $post['Post']['id'], Inflector::slug($post['Post']['title'],'-')));
below are the other links that I tried but failed, I also tried the modifying the routes.php but can't make the proper code to make it work
want to remove action name from url CakePHP
How to remove action name from url in cakephp?
Remove action name from Url in cakephp?
any help/suggestions is appreciated thanks.

Use Id and Named parameter...
On routes.php
define new routes as-
Router::connect('/posts/:id-:title',
array('controller' => 'posts',
'action' => 'view')
);
This new defined routes will match and parse all url containing id and title named parameter..
Important Note:: Do not define new routes after the end of routes.php. Try to define on the middle of the file..
On view
echo $this->Html->link('Hi',array(
'controller' => 'posts',
'action' => 'view',
'id' => 4,
'title' => Inflector::slug('the quick brown fox')
));

Well the solution provided above will fulfill your needs, but still after reading your question one thing comes into my mind... in your controller you are trying to read posts using title I mean this will slow down your system not recommended in programming so use id and increase one more column in your db table for slug(SEO title) which will be used for creating your post urls.
For example:
Post title: This is a test post
Create seo title for this as: this-is-a-test-post-123
Stor seo title in DB as <this-is-a-test-post>
See 123 is your posts ID now change your controller function to get data based on id ie.123, I hope you cn extract 123 from the sting easily...
Note: remember you have to think about this-is-a-123 string also because
they also land in post having id 123.
Use below route for the above solution:
Router::connect('/posts/*', array('controller' => 'posts', 'action' => 'view'),array('pass' => array('title')));
Now in your controller:
You will get string "this-is-a-test-post-123" in $post_title
function view($post_title=null){
$temp = explode('-',$post_title);
$posts_id= end($temp);
$lastKey = end(array_keys($temp));
unset($temp[$lastKey]);
$seoTitle = implode("-",$temp);
//Now compare the above seoTitle with DB seo title for unique urls
}

Related

Zend Framework-Best way to hide Controller name

I am getting one problem in zend framework. I want to hide controller name in my zend framework url.
For Example https://example.com/index/about-us
I want to hide index controller from my url and i want url like
https://example.com/about-us
index is my default controller and about-us is my method
I have tried following code but it doesn't help
i have put this code in /application/Bootstrap.php
public function _initCustomRoute(){
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default-override', $route);
}
please help me to solve this problem.

Laravel 4 - Routes not resetting to root

I'm having an issue where when I go to a route with an href such as
example.com/user/foo
and then click on a link with a href such as
example.com/cart/bar
the URL sets to
example.com/user/cart/bar
and I get an error. The issue is the URL is not resetting to the root directory, but keeps the subdirectory('user') in the URL.
Here's a sample of a link to a user route:
<li>{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}</li>
and the route:
Route::get('/user/{username}', array(
'before' => 'auth',
'as' => '/user/{username}',
'uses' => 'ProfileController#user'
));
the resulting call to the view:
return View::make('profile.user')
->with('user', $user);
at this point, the URL is:
example.com/user/john_smith
But then, let's say I want to view my shopping cart which has an href of:
<li>Cart</li>
and the route:
Route::get('store/cart', array(
'as' => 'get-cart',
'uses' => 'StoreController#getCart'
));
the resulting call to the view:
return View::make('store.cart')->with('products', Cart::contents());
the URL should be:
example.com/store/cart
but instead it's
example.com/user/store/cart
and I get a 'NotFoundHttpException'
You may use the url() helper function to generate an absolute url, for example:
<li>Cart</li>
Check the helper functions.

How to use query string in cake php

I am new in cake php
I made an application in cake php i want to use query string (no exactly query string )some what like this
http://example.net/meditations/index/mins/2
How i can get number 2 (at the end of this above url )
I want this number from url to inside "meditation" controller "index" function.
How I can get that value ?
Thanks
Try this code:
<?php echo $this->Html->link('Link Name', array('controller' => 'meditations', 'action' => 'index', 'mins', 2));?>
You could also do it like below:
http://example.net/meditations/index/?mins=2
Then in your meditations controller index action fetch the value of mins like this:
$_REQUEST['mins']

How to hide everything(id) in the URL in the browser except the site name and controller name in yii?

How to hide/encrypt everything(id) in the URL in the browser except the site name and controller name?
I think UrlManager can do it, but I don't know how ? need url mapping similar in ROR
my url manager code
'urlManager'=>array(
//'urlFormat'=>'path',
'showScriptName'=> false,
'rules'=>array(
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
I like to add a random number between every action(for secure my urls)
ROR eg:
map.connect 'by/:develop_name',
:controller => 'developer',
:action => 'builder_projects'
Please explain step by step.
couple if links I found relate to this
LINK1
LINk2
You just need to specify your application routes appropriately. Before continuing, you should read the URL management chapter of the Yii guide.
What you want to do is use named parameters in your rules, which means that the rule definition would look like this:
'by/<id:\w+>' => 'developer/builder_projects'
This rule takes a URL of the form http://site.com/index.php/by/42 and routes it to the controller developer, action builder_projects with the parameter id equal to whatever 42 (this is what the regular expression \w+ matches).
Routes are specified in your application configuration file as parameters to the urlManager component:
'urlManager' => array(
'urlFormat' => 'path',
'rules' => array(
'by/<id:\w+>' => 'developer/builder_projects'
// more rules
),
),
What you could do is define a helper function which symmetrically encrypts/decrypts:
class Helper {
public static function myCrypt($data, $decrypt = false){
//Logic to encrypt/decrypt
return $result;
}
}
and then when you create urls you can do:
$this->createUrl("myRoute", array("secret_id" => Helper::myCrypt($secret_id)));
and then in the controller action this resolves to you can do this:
public function actionMyRoute($secret_id){
$secret_id = Helper::myCrypt($secret_id, true);
//Do what you need to do with the decrypted id
}
Just make sure your encryption method returns a url safe string.

Managing URL Parameters in Rails

Right now I am finding routing and URL constructing within rails to be semi-confusing. I have currently matched the following for tags that are passed in when displaying/filtering data.
match '/posts/standard/' => 'posts#standard'
match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
match '/posts/standard/:tags' => redirect { |params| "/posts/standard/#{params[:tags].gsub(' ', '+')}" }, :tags => /.+/
However, now I want to add a 'skill' parameter that can only take one state; however, I am very confused by how I want to construct this within my URL. I cannot simply have...
match '/posts/standard/:tags/:skill' => 'posts#standard', as => post_tag, as: => post_skill
So, I am very confused by this at this point, does Rails offer any type of help for constructing URL's?
One way is to just keep your main route
match '/posts/standard/:tags' => 'posts#standard', :as => :post_tag
and handle the additional URL params as params. The url would look like:
/posts/standard/1?skill=something
and it is easy enough to inject the additional params, such as by
link_to post_tag_path(:skill=> 'something')
and your controller would then do
def standard
if params[:skill] == 'something'
...
else
...
end
end
Also, not sure about this, but your first line in your routes 'match '/posts/standard/' => 'posts#standard' may catch all of your routes since there is a match. If this is the case, simply move it to after the first line.

Resources