How can I replace the /posts/view/id with /posts/view/code ?
code is a field in the database that contains 10 random numbers generated using mt_rand() function.
In the PostsController I have this function for viewing posts:
public function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
Now I want to use code instead of id.
Thank you for your answers!
UPDATE:::
Someone from the CakePHP Q&A solved my problem.
I'll put the codes here so if someone needs the same solution, it can be found here.
in your routes.php add this code:
Router::connect('/posts/view/:code', array('controller' => 'news', 'action' => 'view'), array('pass' => array('code')));
in your controller change the view method like this:
public function view($code = null) {
$this->set('post', $this->Post->findByCode($code));
}
Thank you so much!
i used cakePHP
just few times, but i think you should see routing in cakePHP, you should change rewrite of url
I think that would be a URL like /posts/view/id/code. Then using the ID, you grab the code from the database:
public function view($id = null, $code = null) {
$this->Post->id = $id;
//..DO CAKEPHP QUERY TO GET CODE USING ID HERE. ASSIGN TO $code.
$this->Post->code = $code;
$this->set('post', $this->Post->read());
}
Related
I am trying to change the url of my article page so instead of showing the article id I'd like to show the article title.
Currently the URL is as follows;
https://www.example.com/posts/post/32
Where 32 is just a random article id.
Instead I like it to display as follows;
https://www.example.com/posts/post/my-amazing-article
Now I have looked in the laravel documentation and different posts on stackoverflow and tried a bunch of stuff but I'm obviously making a mistake somewhere cus nothing seems to work so im pretty much back where I started.
Blade
Route
Route::get('/posts/post/{id}', [App\Http\Controllers\WelcomeController::class, 'post'])->name('post');
Controller
public function post(request $request){
$id = $request->id;
$article = Tinymce::find($id);
return view('/post')->with('articles, $articles');
}
Now the articles, which are saved in Tinymce, are actually created on a different controller on subdomain.
public function tinymce(Request $request)
{
if(request()->ajax())
{
if($request->article_id == null)
{
$tinymce = new Tinymce;
}else{
$tinymce = Tinymce::find($request->article_id);
}
$tinymce->description = $request->description;
$tinymce->content = $request->myContent;
$tinymce->title = $request->title;
$tinymce->author = $request->author;
$tinymce->publish = $request->publish;
$title = $tinymce->title;
$slug = Str::slug($title, "-");
$tinymce->slug = $slug;
$tinymce->save();
return $tinymce->id;
}
}
As you can see I've turned the title into a slug as I read somewhere that's the way to go to use custom url but I didn't get very far with it.
Any help would be greatly appreciated, thank you.
Explanation:
First, As you said you stored slug into your database. so, that's good.
From Controller to View, you can get that slug into post object.
Blade View : (you have to pass slug in route url)
Route : (make id to slug)
Route::get('/posts/post/{slug}', [App\Http\Controllers\WelcomeController::class, 'post'])->name('post');
Controller : (now you can get slug from the second parameter)
public function post(request $request, $slug = ''){
$article = Tinymce::where('slug', $slug)->first();
return view('/post', compact('article'));
}
Now, you can use access custom URLs using slug.
I Hope, it helps you. #assiemp
I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>
I'm trying to do the same as this site, stackoverflow, do with their URLs.
CakePHP works like this: website/controller/action/
I want to config routing to achieve this:
myWebSite.com/questions/(question_id)/(question)/
eg: myWebSite.com/questions/12874722/cakephp-routing-controller-alias /
I didnt figured it out how to do this bold part of URL.
In your Config/routes.php
Router::connect('/questions/*', array('controller' => 'questions', 'action' => 'view'));
In Controller/QuestionsController.php
view action get question id as
public function view() {
$question_id = isset($this->request->params['pass'][0]) ? $this->request->params['pass'][0] : "";
$question = isset($this->request->params['pass'][1]) ? $this->request->params['pass'][1] : "";
if( empty($question_id) ) {
throw new NotFoundException('Could not find that question');
}
// if $question is empty then get slug from database and redirect to /question_id/question
// Get question details and set
}
Our Yii Framework application has the following defined as part of the UserProfileImages model:
public function getProfileImages($param, $user_id) {
if(isset($param['select']) && $param['select']=='all'){
$profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );
} else {
$profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
}
return $profile_images;
}
How would I wire up the above snippet to a widget in my view to return all the images for a given user?
Bonus Question: Which image rotator do you suggest to render the above?
In your view file, add something like this, assuming that your controller specified $user_id:
$this->widget('UserProfileImagesWidget', array(
"userProfileImages" => UserProfileImages::getProfileImages(
array("select" => "all"),
$user_id
),
"user_id" => $user_id
));
Depending on your MVC philosophy, you could also retrieve the userProfileImages data in the controller and pass that data to your view.
Define a widget like this:
class UserProfileImagesWidget extends CWidget {
public $user_id;
public $userProfileImages = array();
public function run() {
$this->render("userProfileImage");
}
}
Finally, in the userProfileImages.php view file, you can do something like this:
if(!empty($this->userProfileImages)) {
// Your display magic
// You can access $this->user_id
}
As a side note: You might want to change the order of your parameters in getProfileImages. If $user_id is the first parameter, you can leave out $params completely in case you don't want to specify any.
Yii-jedis!
I'm working on some old Yii-project and must to add to them some features. Yii is quite logical framework but it has some things I couldn't understand. Perhaps I haven't understand Yii-way yet. So I'll describe my problem step-by-step. For impatients - briefly question at the end.
Intro: I want to add human-readable URLs to my project.
Now URLs looks like: www.site.com/article/359
And I want them to look like this: www.site.com/article/how-to-make-pretty-urls
Very important: old articles must be available on old format URLs, and new - on new URLs.
Step 1: First, I've updated rewrite rules in config/main.php:
'<controller:\w+>/<id:\S+>' => '<controller>/view',
And I've added new texturl column to article table. So we will store here human-readable-part-of-url for new articles. Then I've updated one article with texturl for tests.
Step 2: Application show articles in actionView of ArticleController so I've added there this code for preproccessing ID parameter:
if (is_numeric($id)) {
// User try to get /article/359
$model = $this->loadModel($id); // Article::model()->findByPk($id);
if ($model->text_url !== null) {
// If article with ID=359 have text url -> redirect to /article/text-url
$this->redirect(array('view', 'id' => $model->text_url), true, 301);
}
} else {
// User try to get /article/text-url
$model = Article::model()->findByAttributes(array('text_url' => $id));
$id = ($model !== null) ? $model->id : null ;
}
And then begin legacy code:
$model = $this->loadModel($id); // Load article by numeric ID
// etc
It works perfectly! But...
Step 3: But we have many actions with ID parameter! What we have to do? Update all actions with that code? I think it's ugly. I've found CController::beforeAction method. Looks good! So I declare beforeAction and place ID preproccessing there:
protected function beforeAction($action) {
$actionToRun = $action->getId();
$id = Yii::app()->getRequest()->getQuery('id');
if (is_numeric($id)) {
$model = $this->loadModel($id);
if ($model->text_url !== null) {
$this->redirect(array('view', 'id' => $model->text_url), true, 301);
}
} else {
$model = Article::model()->findByAttributes(array('text_url' => $id));
$id = ($model !== null) ? $model->id : null ;
}
return parent::beforeAction($action->runWithParams(array('id' => $id)));
}
Yes, it works with both URL-formats, but it executes actionView TWICE and shows page two times! What can I do with this? I've totally confused. Have I choose a right way to solve my problem?
Briefly: Can I proceess ID (GET-parameter) before execute of any actions and then run requested action (once!) with modified only ID parameter?
Last line should be:
return parent::beforeAction($action);
Also to ask you i didnt get your step:3.
As you said you have many controller and you don't need to write code in each file, so you are using beforeAction:
But you have only text_url related to article for all controllers??
$model = Article::model()->findByAttributes(array('text_url' => $id));
===== updated answer ======
I have changed this function, check now.
If $id is not nummeric then we will find it's id using model and set $_GET['id'], so in further controller it will use that numberic id.
protected function beforeAction($action) {
$id = Yii::app()->getRequest()->getQuery('id');
if(!is_numeric($id)) // $id = how-to-make-pretty-urls
{
$model = Article::model()->findByAttributes(array('text_url' => $id));
$_GET['id'] = $model->id ;
}
return parent::beforeAction($action);
}
Sorry, I haven't read it all carefully but have you considered using this extension?